確保特定流程在網格中正好運行一次的建議方法是什麼?Gridgain領導者選舉模式
用例將從遠程源訂閱單個多路複用流,以便跨網格更新數據。我們需要選擇節點來訂閱,並選擇一個新節點來訂閱節點失敗時的訂閱。
在gridgain中是否有任何預構建的模式,或者是通過監聽網格生命週期事件和分佈式CAS操作來解決?
另一個用例是單身作業,必須永久運行,在失敗時遷移到新節點。
謝謝。
確保特定流程在網格中正好運行一次的建議方法是什麼?Gridgain領導者選舉模式
用例將從遠程源訂閱單個多路複用流,以便跨網格更新數據。我們需要選擇節點來訂閱,並選擇一個新節點來訂閱節點失敗時的訂閱。
在gridgain中是否有任何預構建的模式,或者是通過監聽網格生命週期事件和分佈式CAS操作來解決?
另一個用例是單身作業,必須永久運行,在失敗時遷移到新節點。
謝謝。
您可以簡單地選取集羣中最早的節點並在該節點上開始操作(Grid.nodes()將返回網格中的所有節點)。您還應該在其他節點上訂閱發現事件偵聽器,並在最舊節點失敗的情況下讓下一個最早的節點接管。
要檢查一個節點是最老的還是節點可以使用GridNode.order()方法。訂單最小的節點將是最早的。
要收聽發現的事件,您可以使用此代碼:
grid.events().localListen(new GridPredicate<GridEvent>() {
@Override public boolean apply(GridEvent event) {
System.out.println("Event: " + event.name());
return true;
}
}, GridEventType.EVTS_DISCOVERY);
請檢查代碼波紋管。我使用GridGain 6.1.8進行了測試。
import org.gridgain.grid.*;
import org.gridgain.grid.cache.GridCache;
import org.gridgain.grid.cache.GridCacheConfiguration;
import org.gridgain.grid.cache.GridCacheMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
public class GridGainMain {
public static void main(String[] args) throws GridException {
GridConfiguration config = new GridConfiguration();
// Give a name to your grid.
config.setGridName("MyCoolGrid");
// Configure the cache that will be used by the leader election algorithm.
GridCacheConfiguration leaderConf = new GridCacheConfiguration();
leaderConf.setName("leader");
leaderConf.setCacheMode(GridCacheMode.REPLICATED);
config.setCacheConfiguration(leaderConf);
// Start the grid!
try (Grid grid = GridGain.start(config)) {
// Get the local node.
final GridNode localNode = grid.localNode();
// Get the leader cache.
final GridCache<String, String> leaderCache = grid.cache("leader");
// Elect this member as the leader, if no other node was elected yet.
putIfAbsent("leader", localNode.id().toString(), leaderCache);
// ================================================
// Schedule the leader election algorithm.
// The leader election algorithm will elect the oldest grid node as the leader.
// ================================================
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// Get the self ID.
final String selfId = localNode.id().toString();
// Get the cached leader ID.
final String cachedLeaderId = get("leader", leaderCache);
// Get all nodes.
List<GridNode> list = new ArrayList<>(grid.nodes());
// Sort all nodes by natural order.
list.sort((o1, o2) -> (int) (o1.order() - o2.order()));
// Get the ID of the oldest node, which is the leader ID.
final String leaderId = list.get(0).id().toString();
// If the leader ID is not equals to the cached leader ID,
if (!leaderId.equals(cachedLeaderId)) {
// Put the leader ID into cache.
put("leader", leaderId, leaderCache);
}
// If this node is the leader,
if (selfId.equals(leaderId)) {
// =====================================
// Do something! Only this grid node will execute this code.
// =====================================
}
System.out.println("### Self ID: " + selfId
+ ", Order: " + localNode.order()
+ ", Leader ID: " + leaderId);
}
},
// Schedule now.
0L,
// Run the algorithm once every five seconds.
TimeUnit.SECONDS.toMillis(5));
// Remove this in production.
sleep(1, TimeUnit.DAYS);
}
}
private static <T> T get(String key, GridCache<String, T> cache) {
try {
return cache.get(key);
} catch (GridException e) {
return null;
}
}
private static <T> T putIfAbsent(String key, T value, GridCache<String, T> cache) {
try {
return cache.putIfAbsent(key, value);
} catch (GridException e) {
return null;
}
}
private static <T> T put(String key, T value, GridCache<String, T> cache) {
try {
return cache.put(key, value);
} catch (GridException e) {
return null;
}
}
public static void sleep(long duration, TimeUnit unit) {
try {
unit.sleep(duration);
} catch (InterruptedException e) {
// Ignore.
}
}
}
與GridGain 6.2.0-RC2版開始,GridGain有領導人選舉幾種方法:
GridProjection.oldest()
將返回動態投影在集羣中最古老的節點。如果最舊的節點出於任何原因離開羣集,則自動選取下一個最早的節點,以便用戶可以繼續使用最舊的節點而不會中斷。cluster-singleton-service
,per-node-singleton-service
或per-cache-key-singleton-service
。這本身不是領導者選舉,但它可以完全取消領導者選舉的需要,例如,GridGain將保證cluster-singleton-service
在任何時候網格中只有一個該服務實例可用。有關分佈式服務的更多信息,請參閱Distributed Services文檔。