公共组件
Beehive Overview
Beehive is a messaging framework based on go-channels for communication between modules of KubeEdge. A module registered with beehive can communicate with other beehive modules if the name with which other beehive module is registered or the name of the group of the module is known. Beehive supports following module operations:
- Add Module
- Add Module to a group
- CleanUp (remove a module from beehive core and all groups)
Beehive supports following message operations:
- Send to a module/group
- Receive by a module
- Send Sync to a module/group
- Send Response to a sync message
Message Format
Message has 3 parts
- Header:
- ID: message ID (string)
- ParentID: if it is a response to a sync message then parentID exists (string)
- TimeStamp: time when message was generated (int)
- Sync: flag to indicate if message is of type sync (bool)
- Route:
- Source: origin of message (string)
- Group: the group to which the message has to be broadcasted (string)
- Operation: what’s the operation on the resource (string)
- Resource: the resource to operate on (string)
- Content: content of the message (interface{})
Register Module
- On starting edgecore, each module tries to register itself with the beehive core.
- Beehive core maintains a map named modules which has module name as key and implementation of module interface as value.
- When a module tries to register itself with beehive core, beehive core checks from already loaded modules.yaml config file to check if the module is enabled. If it is enabled, it is added in the modules map or else it is added in the disabled modules map.
Channel Context Structure Fields
(Important for understanding beehive operations)
- channels: channels is a map of string(key) which is name of module and chan(value) of message which will used to send message to the respective module.
- chsLock: lock for channels map
- typeChannels: typeChannels is a map of string(key)which is group name and (map of string(key) to chan(value) of message ) (value) which is map of name of each module in the group to the channels of corresponding module.
- typeChsLock: lock for typeChannels map
- anonChannels: anonChannels is a map of string(parentid) to chan(value) of message which will be used for sending response for a sync message.
- anonChsLock: lock for anonChannels map
Module Operations
Add Module
- Add module operation first creates a new channel of message type.
- Then the module name(key) and its channel(value) is added in the channels map of channel context structure.
- Eg: add edged module
coreContext.Addmodule(“edged”)
Add Module to Group
- addModuleGroup first gets the channel of a module from the channels map.
- Then the module and its channel is added in the typeChannels map where key is the group and in the value is a map in which (key is module name and value is the channel).
- Eg: add edged in edged group. Here 1st edged is module name and 2nd edged is the group name.
coreContext.AddModuleGroup(“edged”,”edged”)
CleanUp
- CleanUp deletes the module from channels map and deletes the module from all groups(typeChannels map).
- Then the channel associated with the module is closed.
- Eg: CleanUp edged module
coreContext.CleanUp(“edged”)
Message Operations
Send to a Module
- Send gets the channel of a module from channels map.
- Then the message is put on the channel.
- Eg: send message to edged.
coreContext.Send(“edged”,message)
Send to a Group
- SendToGroup gets all modules(map) from the typeChannels map.
- Then it iterates over the map and sends the message on the channels of all modules in the map.
- Eg: message to be sent to all modules in edged group.
coreContext.SendToGroup(“edged”,message) message will be sent to all modules in edged group.
Receive by a Module
- Receive gets the channel of a module from channels map.
- Then it waits for a message to arrive on that channel and returns the message. Error is returned if there is any.
- Eg: receive message for edged module
msg, err := coreContext.Receive("edged")
SendSync to a Module
- SendSync takes 3 parameters, (module, message and timeout duration)
- SendSync first gets the channel of the module from the channels map.
- Then the message is put on the channel.
- Then a new channel of message is created and is added in anonChannels map where key is the messageID.
- Then it waits for the message(response) to be received on the anonChannel it created till timeout.
- If message is received before timeout, message is returned with nil error or else timeout error is returned.
- Eg: send sync to edged with timeout duration 60 seconds
response, err := coreContext.SendSync("edged",message,60*time.Second)
SendSync to a Group
- Get the list of modules from typeChannels map for the group.
- Create a channel of message with size equal to the number of modules in that group and put in anonChannels map as value with key as messageID.
- Send the message on channels of all the modules.
- Wait till timeout. If the length of anonChannel = no of modules in that group, check if all the messages in the channel have parentID = messageID. If no return error else return nil error.
- If timeout is reached,return timeout error.
- Eg: send sync message to edged group with timeout duration 60 seconds
err := coreContext.SendToGroupSync("edged",message,60*time.Second)
SendResp to a sync message
- SendResp is used to send response for a sync message.
- The messageID for which response is sent needs to be in the parentID of the response message.
- When SendResp is called, it checks if for the parentID of response message , there exists a channel is anonChannels.
- If channel exists, message(response) is sent on that channel.
- Or else error is logged.
coreContext.SendResp(respMessage)