本文整理汇总了Golang中github.com/cs733-iitb/cluster.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: New
func New(rnConfig RaftNodeConfig, jsonFile string) RaftNode {
var rn RaftNode
rn.eventCh = make(chan interface{}, 100)
//rn.timeoutCh = make(chan bool)
rn.shutdownSig = make(chan bool)
rn.commitCh = make(chan CommitInfo, 100)
// rn.parTOs = 0
rn.logDir = rnConfig.logDir
rn.initializeStateMachine(rnConfig)
var err error
rn.nwHandler, err = cluster.New(int(rnConfig.id), jsonFile)
assert(err == nil)
// Register various types to be send on outbox and receive in inbox
gob.Register(VoteReqEv{})
gob.Register(VoteResEv{})
gob.Register(AppendEntriesReqEv{})
gob.Register(AppendEntriesResEv{})
// Set initial election timeout
/*go func() {
time.Sleep(time.Millisecond * time.Duration(RandInt(rn.sm.electionTO)))
rn.timeoutCh <- true
}()*/
rn.timer = time.NewTimer(time.Duration(RandInt(rnConfig.electionTO)) * time.Millisecond)
return rn
}
示例2: NewRN
func NewRN(state State, id int, clusterConfigFileName string, logFileName string, hbTimeout int, timeout int) (*RaftNode, error) {
//TODO Add code for recovery
registerStructs()
srvr, err := cluster.New(id, clusterConfigFileName)
if err != nil {
return nil, err
}
_mainLog, err := log.Open(logFileName)
if err != nil {
return nil, err
}
_stateLog, err := log.Open(logFileName + "_state")
if err != nil {
return nil, err
}
_mainLog.RegisterSampleEntry([]byte{})
_stateLog.RegisterSampleEntry(StateInfo{})
_sm, alarm := NewSm(state, id, srvr.Peers(), hbTimeout, timeout)
rn := RaftNode{appendCh: make(chan Event, 1000), commitCh: make(chan *CommitInfo, 1000), processQuitCh: make(chan bool, 1), isOn: true, sm: _sm, server: srvr, mainLog: _mainLog, stateLog: _stateLog}
rn.timer = time.NewTimer(time.Millisecond * time.Duration(alarm.duration))
go rn.handleEvent()
return &rn, err
}
示例3: GetServer
func GetServer(id int, Cluster []NetConfig) (cluster.Server, error) {
peers := make([]cluster.PeerConfig, len(Cluster))
for i, e := range Cluster {
peers[i] = cluster.PeerConfig{Id: int(e.Id), Address: e.Host + ":" + strconv.Itoa(int(e.Port))}
}
cnf := cluster.Config{Peers: peers}
return cluster.New(id, cnf)
}
示例4: makeRafts
// Generates a cluster of 5 raft nodes with associated tcp ports
func makeRafts() []RaftNode {
var r []RaftNode
for i := 0; i < len(configs.Peers); i++ {
config := NodeConfig{configs, i, "$GOPATH/src/github.com/aakashdeshpande/cs733/assignment3/", 500}
r = append(r, New(config))
r[i].server, _ = cluster.New(i, configs)
}
return r
}
示例5: createNode
//Craetes node, statemachine & Initializes the node.
func createNode(id int, myConf *Config, sm *State_Machine) cluster.Server {
initNode(id, myConf, sm)
//Set up details about cluster nodes form json file.
server, err := cluster.New(id, "config/cluster_config.json")
if err != nil {
panic(err)
}
return server
}
示例6: New
func New(nodeConfig Config) (Node, error) {
clusterConfig := cluster.Config{Peers: NetToPeersConfig(nodeConfig.cluster), InboxSize: 50, OutboxSize: 50}
server, err := cluster.New(nodeConfig.Id, clusterConfig)
if err != nil {
return nil, errors.New("Could not start the messaging service")
}
lg, err := log.Open(nodeConfig.LogDir)
if err != nil {
return nil, errors.New("Could not start log service")
}
lg.RegisterSampleEntry(LogEntry{})
commitChannel := make(chan CommitInfo)
eventChannel := make(chan interface{})
shutdownChan := make(chan int)
initLog := make([]LogEntry, 0)
initLog = append(initLog, LogEntry{0, make([]byte, 0)})
votedFor := -1
term := 0
_, err = os.Stat(strconv.Itoa(nodeConfig.Id) + "_state")
if err == nil {
// State file already exists, so restart read vars from it
term, votedFor = readState(nodeConfig.Id)
// restore log entries from log saved on disk
logLastIndex := lg.GetLastIndex()
if logLastIndex != -1 {
//logger.Println(nodeConfig.Id, " : Last index on disk : ", logLastIndex)
for i := 0; int64(i) < logLastIndex; i++ {
entry, _ := lg.Get(int64(i))
initLog = append(initLog, entry.(LogEntry))
}
}
}
sm := StateMachine{nodeConfig.Id, getPeers(nodeConfig.cluster), term,
votedFor, 1, initLog, make(map[int]int), make(map[int]int),
0, nodeConfig.ElectionTimeout, make(map[int]int), -1}
rn := RaftNode{sm: sm, server: server, lg: lg, commitChannel: commitChannel, eventChannel: eventChannel, shutDownChan: shutdownChan}
timerFunc := func(eventChannel chan interface{}) func() {
return func() {
rn.eventChannel <- TimeoutEv{}
}
}
rn.timer = time.AfterFunc(time.Duration(random(sm.timeout, 2*sm.timeout))*time.Millisecond, timerFunc(rn.eventChannel))
rn.commitLock = &sync.RWMutex{}
gob.Register(AppendEntriesReqEv{})
gob.Register(AppendEntriesRespEv{})
gob.Register(VoteReqEv{})
gob.Register(VoteRespEv{})
go rn.ProcessEvents()
return &rn, nil
}
示例7: makeRafts
func makeRafts() []RaftNode {
// votedForDB, _ = leveldb.OpenFile(PATH+"/votedFor", nil)
var nodes []RaftNode
for i := 0; i < len(configs.Peers); i++ {
config := Config{configs, configs.Peers[i].Id, PATH + "", 550, 50}
server, _ := cluster.New(configs.Peers[i].Id, configs)
nodes = append(nodes, New(config, server))
}
return nodes
}
示例8: makerafts
// makerafts function will make 5 rodeNodes .It will associate a stateMAchine to all nodes.
func makerafts() []*RaftNode {
raftset := make([]*RaftNode, 5)
for i := 0; i < 5; i++ {
raft, err := cluster.New(i, "peer.json")
checkError(err)
node := initialize(raft, i)
raftset[i] = node
}
return raftset
}
示例9: New
func New(conf Config) Node {
var (
rn RaftNode
rsmLog []LogEntry
peerIds []int
hasVoted map[int]int
nextIndex map[int]int64
matchIndex map[int]int64
ClustConfig cluster.Config
)
// initlisation of other raft node variables
rn.IsWorking = true
rn.TimeoutTimer = time.NewTimer(time.Duration(randRange(conf.ElectionTimeout, 2*conf.ElectionTimeout)) * time.Millisecond)
<-rn.TimeoutTimer.C
//fmt.Println(<-rn.TimeoutTimer.C)
rn.AppendEventCh = make(chan Event, 100)
//rn.TimeoutEventCh = make(chan Event, 100)
rn.CommitCh = make(chan CommitInfo, 100)
ClustConfig = GetClusterConfig(conf)
rn.NetServer, _ = cluster.New(conf.Id, ClustConfig)
rn.LogFile, _ = log.Open(conf.LogFileDir)
// initilisation of state machine
peerIds = getPeerIds(conf)
hasVoted = getVotesRcvd(peerIds)
nextIndex = getNextIndex(peerIds)
matchIndex = getMatchIndex(peerIds)
registerStructs()
rsmLog, _ = rn.getRsmLog()
//rsmState, err = rn.getRsmState();
rn.SM.init( /* currTerm */ 0,
/* votedFor */ -1,
/* Log */ rsmLog,
/* selfId */ conf.Id,
/* peerIds */ peerIds,
/* electionAlarm */ conf.ElectionTimeout,
/* heartbeatAlarm */ conf.HeartbeatTimeout,
/* lastMatchIndex */ -1,
/* currState --Follower*/ "follower",
/* commitIndex */ -1,
/* leaderId */ -1,
/* lastLogIndex */ -1,
/* lastLogTerm */ 0,
/* votedAs */ hasVoted,
/* nextIndex */ nextIndex,
/* matchIndex */ matchIndex)
go rn.ProcessNodeEvents()
return &rn
}
示例10: New
func New(RaftNode_config RaftConfig) RaftNode {
//make raftnode object and set it
var rn RaftNode
rn.rc = RaftNode_config
rn.sm = InitializeStateMachine(RaftNode_config)
rn.EventCh = make(chan interface{}, 1000)
rn.CommitCh = make(chan CommitInfo, 1000)
rn.timer = time.NewTimer(time.Duration(RaftNode_config.ElectionTimeout) * time.Millisecond)
rn.quit = make(chan bool)
rn.srvr, _ = cluster.New(RaftNode_config.Id, "Config.json") //make server object for communication
// register events
gob.Register(VoteRequestEvent{})
gob.Register(VoteResponseEvent{})
gob.Register(AppendEntriesRequestEvent{})
gob.Register(AppendEntriesResponseEvent{})
return rn
}
示例11: NewFsNode
func NewFsNode(state State, id int, clusterConfigFileName string, logFileName string, hbTimeout int, timeout int, _pubaddr string) (*FsNode, error) {
//TODO Add code for recovery
registerStructs()
srvr, err := cluster.New(id, clusterConfigFileName)
if err != nil {
return nil, err
}
_mainLog, err := log.Open(logFileName)
if err != nil {
return nil, err
}
_stateLog, err := log.Open(logFileName + "_state")
if err != nil {
return nil, err
}
_mainLog.RegisterSampleEntry([]byte{})
_stateLog.RegisterSampleEntry(StateInfo{})
_sm, alarm := NewSm(state, id, srvr.Peers(), hbTimeout, timeout)
fsn := FsNode{appendCh: make(chan Event, 1000), commitCh: make(chan *CommitInfo, 1000), processQuitCh: make(chan bool, 1), frontEndQuitCh: make(chan bool, 1), backEndQuitCh: make(chan bool, 1), isOn: true, sm: _sm, server: srvr, mainLog: _mainLog, stateLog: _stateLog, dict: make(map[string]*FileStruct, 1000), pubaddr: _pubaddr, connMap: make(map[int64]*net.Conn)}
fsn.timer = time.NewTimer(time.Millisecond * time.Duration(alarm.duration))
addr, err := net.ResolveTCPAddr("tcp4", fsn.pubaddr)
printErr(err)
fsn.socket, err = net.ListenTCP("tcp", addr)
printErr(err)
go fsn.frontEndMain()
go fsn.backEndMain()
go fsn.handleEvent()
return &fsn, err
}
示例12: main
func main() {
var configs []raftnode.Config
tmp_cnfg, _ := ioutil.ReadFile("configs.json")
err := json.Unmarshal(tmp_cnfg, &configs)
check(err)
// var cluster_config cluster.Config
// tmp_clusterConfig,_ := ioutil.ReadFile("clusterconfig.json")
// err = json.Unmarshal(tmp_clusterConfig,&cluster_config)
// check(err)
id, err1 := strconv.Atoi(os.Args[1])
check(err1)
//
server, err := cluster.New(id, "cluster_test_config.json")
//fmt.Println("port: ",configs[id-1].Cluster[id-1].Port,id)
//ioutil.WriteFile("welcome.txt",[]byte(s),0644)
serverMain(id, (configs[id-1]), server)
//serverMain(1,configs[0],mck)
}
示例13: New
func New(config Config, jsonFile string) (rnode RaftNode) {
rnode.sm.serverId = config.Id
//fmt.Printf("In New k value: %v\n", len(config.cluster)-1)
rnode.sm.peerIds = make([]int64, len(config.cluster)-1, len(config.cluster)-1)
k := 0
for _, peer := range config.cluster {
//fmt.Printf("In New k value: %d\n", k)
if peer.Id != config.Id {
rnode.sm.peerIds[k] = peer.Id
k++
}
}
rnode.sm.majority = int64(math.Ceil(float64(len(config.cluster)) / 2.0))
rnode.sm.commitIndex = -1
rnode.logfile = config.LogDir + "/" + "logfile"
lg, err := log.Open(rnode.logfile)
lg.RegisterSampleEntry(logEntry{})
assert(err == nil)
defer lg.Close()
if lg.GetLastIndex() == -1 {
rnode.sm.log = []logEntry{}
} else {
i := lg.GetLastIndex()
for j := int64(0); j <= i; j++ {
data, _ := lg.Get(j)
rnode.sm.log = append(rnode.sm.log, data.(logEntry))
}
}
rnode.sm.nextIndex = make([]int64, len(config.cluster)-1, len(config.cluster)-1)
for n := 0; n < len(config.cluster)-1; n++ {
rnode.sm.nextIndex[n] = int64(len(rnode.sm.log))
}
rnode.sm.matchIndex = make([]int64, len(config.cluster)-1, len(config.cluster)-1)
for m := 0; m < len(config.cluster)-1; m++ {
rnode.sm.matchIndex[m] = -1
}
rnode.statefile = config.LogDir + "/" + "statefile"
currstate, err := log.Open(rnode.statefile)
currstate.RegisterSampleEntry(NodePers{})
assert(err == nil)
defer currstate.Close()
if currstate.GetLastIndex() == -1 {
rnode.sm.currentTerm = int64(0)
rnode.sm.currentState = "follower"
} else {
i := currstate.GetLastIndex()
h, _ := currstate.Get(i)
rnode.sm.currentTerm = h.(NodePers).CurrentTerm
rnode.sm.currentState = h.(NodePers).CurrentState
rnode.sm.votedFor = h.(NodePers).VotedFor
}
rnode.sm.totalvotes = int64(0)
rnode.sm.novotes = int64(0)
rnode.eventch = make(chan interface{}, 1000)
rnode.commitch = make(chan CommitInfo, 1000)
rnode.endch = make(chan bool)
//rnode.timeoutch = make(chan TimeoutEv)
//rnode.resettimer = 0
rnode.sm.ElectionTimeout = config.ElectionTimeout
rnode.sm.HeartbeatTimeout = config.HeartbeatTimeout
gob.Register(AppendEv{})
gob.Register(AppendEntriesReqEv{})
gob.Register(AppendEntriesRespEv{})
gob.Register(TimeoutEv{})
gob.Register(VoteReqEv{})
gob.Register(VoteRespEv{})
rnode.timer = time.NewTimer(time.Duration(config.ElectionTimeout) * time.Millisecond)
var err3 error
rnode.sm_messaging, err3 = cluster.New(int(config.Id), jsonFile)
if err3 != nil {
fmt.Printf("Error in sm_messaging.")
}
return
}
示例14: New
func New(config Config) *RaftNode {
Register()
var raft RaftNode
directories := config.LogDir + strconv.Itoa(config.Id)
lg, _ := log.Open(directories)
lastindex := lg.GetLastIndex()
intlastindex := int(lastindex)
mypeers := make([]int, noOfServers)
myLog := make([]Log, 0)
myLog = append(myLog, Log{0, []byte("hello")})
mynextIndex := make([]int, noOfServers)
mymatchIndex := make([]int, noOfServers)
myVoteReceived := make([]int, noOfServers)
statestore := "statestore" + strconv.Itoa(config.Id)
file, e := ioutil.ReadFile("./" + statestore)
if e != nil {
fmt.Printf("File error: %v\n", e)
os.Exit(1)
}
var jsontype JsonStateStore
json.Unmarshal(file, &jsontype)
var wait sync.WaitGroup
raft.wg = &wait
configCluster := cluster.Config{
Peers: []cluster.PeerConfig{},
InboxSize: config.InboxSize,
OutboxSize: config.OutboxSize,
}
for i := 0; i < len(config.Cluster); i++ {
configCluster.Peers = append(configCluster.Peers, cluster.PeerConfig{Id: config.Cluster[i].Id, Address: fmt.Sprint(config.Cluster[i].Host, ":", config.Cluster[i].Port)})
mypeers[i] = config.Cluster[i].Id
}
s := RaftStateMachine{
Term: jsontype.Term,
VotedFor: jsontype.VotedFor,
State: "follower",
MyID: config.Id,
PeerID: mypeers,
Log: myLog,
CommitIndex: 0,
LeaderID: -1,
NextIndex: mynextIndex,
MatchIndex: mymatchIndex,
VoteReceived: myVoteReceived,
HeartbeatTimeout: config.HeartbeatTimeout,
ElectionTimeout: config.ElectionTimeout,
}
raft.server = s
for i := 0; i <= intlastindex; i++ {
newLog, _ := lg.Get(int64(i))
raft.server.Log = append(raft.server.Log, Log{Term: 0, Data: newLog})
}
eventChannel := make(chan Event, 1000)
commitChannel := make(chan *Commit, 1000)
shutdownChannel := make(chan Event, 1000)
raft.eventCh = eventChannel
raft.commitCh = commitChannel
raft.shutdownCh = shutdownChannel
clusterServer, _ := cluster.New(config.Id, configCluster)
raft.serverOfCluster = clusterServer
raft.log = lg
raft.LogDir = config.LogDir
return &raft
}
示例15: New1
func New1(myid int, configuration interface{}) (n Node, err error) {
Raft := new(RaftNode)
n = Raft
var config *Config
var pdata []byte
if config, err = ConfigRaft(configuration); err != nil {
return nil, err
}
config.Id = myid
pdata, err = ioutil.ReadFile(fmt.Sprintf("persistent_store_%d", myid))
if err != nil {
panic(err)
}
p := strings.Split(string(pdata), " ")
p[0] = strings.TrimSpace(p[0])
p[1] = strings.TrimSpace(p[1])
smterm, _ := strconv.Atoi(p[1])
smvotedfor, _ := strconv.Atoi(p[0])
server, err := cluster.New(myid, "cluster_test_config.json")
if err != nil {
panic(err)
}
Raft.srv = server
Raft.config = config
nodesid := [4]int{}
j := 0
for i := 0; i <= 4; i++ {
if config.Cluster[i].Id != myid {
nodesid[j] = config.Cluster[i].Id
j = j + 1
}
}
next := []int{1, 1, 1, 1, 1, 1}
sm = &StateMachine{id: myid, term: smterm, LastLogIndex: 0, CommitIndex: 0, PrevLogIndex: -1, nextIndex: next, peers: nodesid, votedfor: smvotedfor, status: "Follower", LeaderID: 0, ElectionTimeout: config.ElectionTimeout, HeartBeatTimeout: config.HeartbeatTimeout}
Raft.sm = sm
rand.Seed(time.Now().UnixNano())
RandomNo := rand.Intn(500)
Raft.Timer = time.AfterFunc(time.Duration(config.ElectionTimeout+RandomNo)*time.Millisecond, func() { Raft.timeoutCh <- TimeoutEv{} })
Raft.Heart_Beat_Timer = time.NewTicker(time.Duration(config.HeartbeatTimeout) * time.Millisecond)
go Raft.processEvents()
go Raft.sm.ProcessEvent(make([]interface{}, 1))
Events := make(chan Event, 10)
commitchannel := make(chan CommitInfo, 1)
timeout := make(chan interface{})
Raft.eventCh = Events
Raft.CommitCh = commitchannel
Raft.timeoutCh = timeout
config.LogDir = fmt.Sprintf("log_File_%d", myid)
lg, _ := log1.Open(config.LogDir)
if lg == nil {
fmt.Println("Error opening Log File")
}
Raft.lg = *lg
Raft.sm.log = &Raft.lg
//Raft.lg.TruncateToEnd(0)
Raft.lg.RegisterSampleEntry(Log{})
er := Raft.lg.Append(Log{Logindex: 0, Term: 0, Entry: []byte("foo")})
if er != nil {
fmt.Println(er)
}
return
}