PageRenderTime 24ms CodeModel.GetById 14ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 1ms

/hazelcast/src/main/java/com/hazelcast/impl/partition/BackupSafeMemberGroupFactory.java

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 60 lines | 34 code | 9 blank | 17 comment | 7 complexity | ff3043086f55a32fecd37e52cdcf141e MD5 | raw file
 1/*
 2 * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
 3 *
 4 * Licensed under the Apache License, Version 2.0 (the "License");
 5 * you may not use this file except in compliance with the License.
 6 * You may obtain a copy of the License at
 7 *
 8 * http://www.apache.org/licenses/LICENSE-2.0
 9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.hazelcast.impl.partition;
18
19import com.hazelcast.impl.MemberImpl;
20
21import java.util.Collection;
22import java.util.Iterator;
23import java.util.LinkedList;
24import java.util.Set;
25
26abstract class BackupSafeMemberGroupFactory implements MemberGroupFactory {
27
28    public final Collection<MemberGroup> createMemberGroups(final Collection<MemberImpl> allMembers) {
29        final Collection<MemberImpl> members = removeLiteMembers(allMembers);
30        final Collection<MemberGroup> groups = createInternalMemberGroups(members);
31        if (groups.size() == 1 && members.size() > 1) {
32            // If there are more than one members and just one group
33            // then split members into two groups to guarantee at least the first backup.
34            MemberGroup group1 = groups.iterator().next();
35            MemberGroup group2 = new DefaultMemberGroup();
36            final int sizePerGroup = group1.size() / 2;
37
38            final Iterator<MemberImpl> iter = group1.iterator();
39            while (group2.size() < sizePerGroup && iter.hasNext()) {
40                group2.addMember(iter.next());
41                iter.remove();
42            }
43            groups.add(group2);
44        }
45        return groups;
46    }
47
48    protected abstract Set<MemberGroup> createInternalMemberGroups(final Collection<MemberImpl> allMembers) ;
49
50    private Collection<MemberImpl> removeLiteMembers(Collection<MemberImpl> members) {
51        final Collection<MemberImpl> result = new LinkedList<MemberImpl>();
52        for (MemberImpl member : members) {
53            if (!member.isLiteMember()) {
54                result.add(member);
55            }
56        }
57        return result;
58    }
59
60}