Пример #1
0
//all past commited command will come into replicators inbox
func ListenInBox(Replicator Raft.Replicator) {
	count := 0
	var xcel Xcel
	for {
		select {
		case t := <-Replicator.Inbox():
			count++
			fmt.Println("enter receinved:", count)
			buf := bytes.NewBufferString(string(*t))
			dec := gob.NewDecoder(buf)

			err := dec.Decode(&xcel)
			if err != nil {
				panic(fmt.Sprintf("decode:", err))
			} else {
				ApplyOldCommandTOSM(&xcel)

			}

		}
	}

}
Пример #2
0
// create new xceldb
func main() {

	var Cluster cluster.Server
	var Replicator Raft.Replicator
	Id := flag.Int("id", 1, "a int")
	flag.Parse()
	configFname := GetPath() + "/src/github.com/nilangshah/XcelDB/config.xml"
	Confname := GetPath() + "/src/github.com/nilangshah/XcelDB/c_config.xml"
	LogPath := GetPath() + "/src/github.com/nilangshah/XcelDB/Raftlog" + strconv.Itoa(*Id)

	//intialize state machine
	xcelSM := NewStateMachine()

	//intialize replicator and cluster
	Cluster = cluster.New(*Id, Confname)
	Replicator = Raft.New(Cluster, LogPath)

	// listen inbox for past commited commands
	go ListenInBox(Replicator)

	//initialize db
	xcelDB = &XcelDB{
		xcelId:         uint64(*Id),
		xcelSM:         xcelSM,
		xcelReplicator: Replicator,
	}

	//start replicator
	Replicator.Start()

	//intialize xceldb

	//read config file
	var Jsontype Jsonobject
	file, e := ioutil.ReadFile(configFname)
	if e != nil {
		panic("File error: " + e.Error())
	}

	xml.Unmarshal(file, &Jsontype)
	//fmt.Println(xml.Marshal(&Jsontype))
	// store configuration
	count := 0
	xcelDB.xcelPeers = make([]uint64, len(Jsontype.Object.Servers))
	xcelDB.xcelPeermap = make(map[uint64]string, len(Jsontype.Object.Servers))
	for i := range Jsontype.Object.Servers {
		if Jsontype.Object.Servers[i].Id == uint64(*Id) {

		} else {
			xcelDB.xcelPeers[count] = Jsontype.Object.Servers[i].Id
			count++
		}
		xcelDB.xcelPeermap[Jsontype.Object.Servers[i].Id] = Jsontype.Object.Servers[i].Host

	}

	//start http server
	http.HandleFunc("/", kvHandler)
	select {
	case <-time.After(2 * time.Second):
		fmt.Println(xcelDB.xcelReplicator.IsRunning())

	}
	http.ListenAndServe(xcelDB.xcelPeermap[uint64(*Id)], nil)

}