本文整理匯總了Golang中github.com/owlfish/forestbus-server/model.Messages.Slice方法的典型用法代碼示例。如果您正苦於以下問題:Golang Messages.Slice方法的具體用法?Golang Messages.Slice怎麽用?Golang Messages.Slice使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/owlfish/forestbus-server/model.Messages
的用法示例。
在下文中一共展示了Messages.Slice方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Append
//.........這裏部分代碼省略.........
clog.node_log("Previous term of %v doesn't match previousMessageTerm of %v\n", previousTerm, previousMessageTerm)
return nextIndex, false, nil
}
//clog.node_log("Our last index is %v. Leader thinks our last index is %v and we are trying to append %v messages\n", lastIndex, previousIndex, msgs.GetCount())
// How many messages overlap? If len(msgs) is shorter than the gap between lastIndex and previousIndex, use that.
// How many messages overlap? If we have 10 messages and previousIndex was 8 then we have two left to check (9 & 10)
// How many messages overlap? If we have a lastIndex of 10 and previousIndex of 8 then we have two left to check (9 & 10)
msgsToCheck := msgs.GetCount()
// We have already checked the first message, does the rest push us beyond the lastIndex?
// E.g. len (msgs) = 2, previousIndex = 5, lastIndex = 7.
if (int64(msgsToCheck) + previousIndex) >= lastIndex {
msgsToCheck = int(lastIndex - previousIndex)
}
// Next question - do all messages beyond the previousMessage match the contents of the new messages?
checkMessagesIndex := previousIndex
if msgsToCheck > 0 {
overlappingMessages, err := clog.getAtLeastMessages(checkMessagesIndex+1, int64(msgsToCheck))
clog.node_log("Retrieved %v messages from index %v to check for overlaps (needed at least %v)\n", overlappingMessages.GetCount(), checkMessagesIndex+1, msgsToCheck)
if err != nil {
return nextIndex, false, err
}
for i := 0; i < overlappingMessages.GetCount(); i++ {
// Check terms match for the given index
// TODO: Check payload and CRC as well?
checkMessagesIndex++
//clog.node_log("Loop index %v, overlapMessageCount: %v\n", i, overlapMessageCount)
overlappingMessagesTerm, err := overlappingMessages.GetMessageTerm(i)
if err != nil {
return nextIndex, false, err
}
msgsTerm, err := msgs.GetMessageTerm(overlapMessageCount)
if err != nil {
return nextIndex, false, err
}
if overlappingMessagesTerm != msgsTerm {
// We need to truncate from here to the end of the log.
clog.node_log("Truncating log due to mismatching terms to index %v.\n", checkMessagesIndex)
clog.log.TruncateMessages(int64(checkMessagesIndex))
break
}
// If we get this far then we have an overlapping message
overlapMessageCount++
}
}
} else {
if previousIndex != 0 {
clog.node_log("Empty log, but previousIndex of %v given by leader\n", previousIndex)
return nextIndex, false, nil
}
// We have our first messages - use AppendFirstMessages to set the index correctly (first message isn't always 1)
if msgs.GetCount() > 0 {
//clog.node_log("Attempting to append %v messages\n", len(msgsToAppend))
lastID, err := clog.log.AppendFirstMessages(msgs, leaderFirstIndex)
if err != nil {
clog.node_log("Error attempting to Append messages to the log: %v\n", err)
return nextIndex, false, nil
} else if clog.syncPolicy == WRITE_SYNC {
err = clog.log.Sync()
if err != nil {
clog.node_log("Error attempting to sync Append messages to the log: %v\n", err)
return nextIndex, false, nil
}
}
nextIndex = lastID + 1
return nextIndex, true, nil
}
}
// All messages beyond overlapMessageCount should now be appended.
msgsToAppend, err := msgs.Slice(overlapMessageCount, msgs.GetCount())
if err != nil {
clog.node_log("Error slicing overlapping messages: %v\n", err)
return nextIndex, false, nil
}
if msgsToAppend.GetCount() > 0 {
//clog.node_log("Attempting to append %v messages\n", len(msgsToAppend))
lastID, err := clog.log.AppendMessages(msgsToAppend)
if err != nil {
clog.node_log("Error attempting to Append messages to the log: %v\n", err)
return nextIndex, false, nil
} else if clog.syncPolicy == WRITE_SYNC {
err = clog.log.Sync()
if err != nil {
clog.node_log("Error attempting to sync Append messages to the log: %v\n", err)
return nextIndex, false, nil
}
}
nextIndex = lastID + 1
} else {
//clog.node_log("Call to append, but not message to add.\n")
}
return nextIndex, true, nil
}