Ejemplo n.º 1
0
        public ReplHandler getReplicatedDatabase_Fresh(string server_guid)
        {
            ServerContext ctx = new ServerContext();
            ctx.server_guid = server_guid;
            ctx.connector = connector;

            return ReplHandler.InitFresh(new StepsStageSnapshot(new StepsStageSubset(new RecordKeyType_String(ctx.server_guid), this.db)), ctx);
        }
Ejemplo n.º 2
0
        public ReplHandler getReplicatedDatabase_Join(string new_server_guid, string join_server_guid)
        {
            ServerContext ctx = new ServerContext();
            ctx.server_guid = new_server_guid;
            ctx.connector = connector;

            return ReplHandler.InitJoin(
                new StepsStageSnapshot(new StepsStageSubset(new RecordKeyType_String(ctx.server_guid), this.db)),
                ctx,
                join_server_guid);
        }
Ejemplo n.º 3
0
 public static ReplHandler InitResume(IStepsSnapshotKVDB db, ServerContext ctx)
 {
     ReplHandler repl = new ReplHandler(db, ctx);
     return repl;
 }
Ejemplo n.º 4
0
        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;
        }
Ejemplo n.º 5
0
        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;
        }
Ejemplo n.º 6
0
        public ReplHandler(IStepsSnapshotKVDB db, ServerContext ctx)
        {
            this.next_stage = db;
            this.my_repl_interface = new MyReplConnection(this);
            this.rnd = new Random();
            this.pusher = new ReplPusher(this);
            this.ctx = ctx;

            try {

                var di_rk = new RecordKey()
                    .appendKeyPart("_config")
                    .appendKeyPart("DATA-INSTANCE-ID");
                var rec = db.FindNext(di_rk, true);
                if (di_rk.CompareTo(rec.Key) != 0) {
                    throw new Exception(
                        String.Format("ReplHandler {0} , not able to fetch DATA-INSTANCE-ID", ctx.server_guid));
                }
                this.data_instance_id = rec.Value.ToString();
                Console.WriteLine("ReplHandler - {0}: data_instance_id {1}",
                    ctx.server_guid, data_instance_id);
            } catch (KeyNotFoundException) {
                throw new Exception("no data instance ID, try InitResume or InitJoin");
            }

            // check server_guid matches?

            // register ourself
            ctx.connector.registerServer(ctx.server_guid, this.my_repl_interface);

            // startup our background task
            worker = new Thread(delegate() {
                this.workerThread();
            });
            worker.Start();
        }