public static ReplHandler InitJoin(IStepsSnapshotKVDB db, ServerContext ctx, string seed_name) { // connect to the other server, get his instance id, exchange seeds IReplConnection seed = ctx.connector.getServerHandle(seed_name); JoinInfo join_info = seed.requestToJoin(ctx.server_guid); // record the join result db.setValue(new RecordKey() .appendKeyPart("_config") .appendKeyPart("DATA-INSTANCE-ID"), RecordUpdate.WithPayload(join_info.data_instance_id)); // init a clean log db.setValue(new RecordKey() .appendKeyPart("_logs") .appendKeyPart(ctx.server_guid) .appendKeyPart(new RecordKeyType_Long(0)), RecordUpdate.WithPayload(new byte[0])); foreach (var seed_server in join_info.seed_servers) { db.setValue(new RecordKey() .appendKeyPart("_config") .appendKeyPart("seeds") .appendKeyPart(seed_server), RecordUpdate.WithPayload("")); } Console.WriteLine("InitJoin: server ({0}) joining seeds ({1})", ctx.server_guid, String.Join(",", join_info.seed_servers)); ReplHandler repl = new ReplHandler(db, ctx); return repl; }
public static ReplHandler InitFresh(IStepsSnapshotKVDB db, ServerContext ctx) { // init fresh // record our instance ID db.setValue(new RecordKey() .appendKeyPart("_config") .appendKeyPart("MY-SERVER-ID"), RecordUpdate.WithPayload(ctx.server_guid)); // create and record a new instance ID db.setValue(new RecordKey() .appendKeyPart("_config") .appendKeyPart("DATA-INSTANCE-ID"), RecordUpdate.WithPayload(Lsd.numberToLsd(ReplHandler.myrnd.Next(), 15))); // record the "start of fresh log" record db.setValue(new RecordKey() .appendKeyPart("_logs") .appendKeyPart(ctx.server_guid) .appendKeyPart(new RecordKeyType_Long(0)), RecordUpdate.WithPayload(new byte[0])); // record ourself as a seed/log db.setValue(new RecordKey() .appendKeyPart("_config").appendKeyPart("seeds").appendKeyPart(ctx.server_guid), RecordUpdate.WithPayload("")); ReplHandler repl = new ReplHandler(db, ctx); repl.state = ReplState.active; // TODO: is this the right way to become active? return repl; }