Group Membership¶
Basic operations¶
One of the feature provided by the coordinator is the ability to handle group membership. Once a group is created, any coordinator can join the group and become a member of it. Any coordinator can be notified when a members joins or leaves the group.
# Copyright (C) 2020 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import uuid
from tooz import coordination
coordinator = coordination.get_coordinator('zake://', b'host-1')
coordinator.start()
# Create a group
group = bytes(str(uuid.uuid4()).encode('ascii'))
request = coordinator.create_group(group)
request.get()
# Join a group
request = coordinator.join_group(group)
request.get()
coordinator.stop()
Note that all the operation are asynchronous. That means you cannot be sure
that your group has been created or joined before you call the
tooz.coordination.CoordAsyncResult.get()
method.
You can also leave a group using the
tooz.coordination.CoordinationDriver.leave_group()
method. The list of
all available groups is retrievable via the
tooz.coordination.CoordinationDriver.get_groups()
method.
Watching Group Changes¶
It’s possible to watch and get notified when the member list of a group changes. That’s useful to run callback functions whenever something happens in that group.
# Copyright (C) 2020 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import uuid
from tooz import coordination
coordinator = coordination.get_coordinator('zake://', b'host-1')
coordinator.start()
# Create a group
group = bytes(str(uuid.uuid4()).encode('ascii'))
request = coordinator.create_group(group)
request.get()
def group_joined(event):
# Event is an instance of tooz.coordination.MemberJoinedGroup
print(event.group_id, event.member_id)
coordinator.watch_join_group(group, group_joined)
coordinator.stop()
Using tooz.coordination.CoordinationDriver.watch_join_group()
and
tooz.coordination.CoordinationDriver.watch_leave_group()
your
application can be notified each time a member join or leave a group. To
stop watching an event, the two methods
tooz.coordination.CoordinationDriver.unwatch_join_group()
and
tooz.coordination.CoordinationDriver.unwatch_leave_group()
allow to
unregister a particular callback.