PageRenderTime 61ms CodeModel.GetById 16ms app.highlight 40ms RepoModel.GetById 2ms app.codeStats 0ms

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

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 188 lines | 146 code | 24 blank | 18 comment | 18 complexity | cf2f7e152f209748e41b5ce9aa7be923 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.cluster.MemberInfo;
 20import com.hazelcast.nio.Address;
 21import com.hazelcast.nio.Connection;
 22import com.hazelcast.nio.DataSerializable;
 23import com.hazelcast.util.Clock;
 24
 25import java.io.DataInput;
 26import java.io.DataOutput;
 27import java.io.IOException;
 28import java.util.*;
 29
 30/**
 31 * @mdogan 5/7/12
 32 */
 33public class PartitionRuntimeState implements DataSerializable {
 34
 35    protected ArrayList<MemberInfo> members = new ArrayList<MemberInfo>(100);
 36    protected Collection<ShortPartitionInfo> partitionInfos = new LinkedList<ShortPartitionInfo>();
 37    private long masterTime = Clock.currentTimeMillis();
 38    private int version;
 39    private Connection connection;
 40
 41    public PartitionRuntimeState() {
 42        super();
 43    }
 44
 45    public long getMasterTime() {
 46        return masterTime;
 47    }
 48
 49    public PartitionRuntimeState(final Collection<MemberInfo> memberInfos,
 50                                 final PartitionInfo[] partitions,
 51                                 final long masterTime, int version) {
 52        this.masterTime = masterTime;
 53        this.version = version;
 54        final Map<Address, Integer> addressIndexes = new HashMap<Address, Integer>(memberInfos.size());
 55        int memberIndex = 0;
 56        for (MemberInfo memberInfo : memberInfos) {
 57            addMemberInfo(memberInfo, addressIndexes, memberIndex);
 58            memberIndex++;
 59        }
 60        setPartitions(partitions, addressIndexes);
 61    }
 62
 63    public ArrayList<MemberInfo> getMembers() {
 64        return members;
 65    }
 66
 67    protected void addMemberInfo(MemberInfo memberInfo, Map<Address, Integer> addressIndexes, int memberIndex) {
 68        members.add(memberIndex, memberInfo);
 69        addressIndexes.put(memberInfo.getAddress(), memberIndex);
 70    }
 71
 72    protected void setPartitions(PartitionInfo[] partitions, Map<Address, Integer> addressIndexes) {
 73        for (PartitionInfo partition : partitions) {
 74            ShortPartitionInfo spi = new ShortPartitionInfo(partition.getPartitionId());
 75            for (int i = 0; i < PartitionInfo.MAX_REPLICA_COUNT; i++) {
 76                Address address = partition.getReplicaAddress(i);
 77                if (address == null) {
 78                    spi.addressIndexes[i] = -1;
 79                } else {
 80                    Integer knownIndex = addressIndexes.get(address);
 81                    spi.addressIndexes[i] = (knownIndex == null) ? -1 : knownIndex;
 82                }
 83            }
 84            partitionInfos.add(spi);
 85        }
 86    }
 87
 88    public PartitionInfo[] getPartitions() {
 89        int size = partitionInfos.size();
 90        PartitionInfo[] partitions = new PartitionInfo[size];
 91        for (ShortPartitionInfo spi : partitionInfos) {
 92            PartitionInfo partition = new PartitionInfo(spi.partitionId, null);
 93            int[] addressIndexes = spi.addressIndexes;
 94            for (int c = 0; c < addressIndexes.length; c++) {
 95                int index = addressIndexes[c];
 96                if (index != -1) {
 97                    partition.setReplicaAddress(c, members.get(index).getAddress());
 98                }
 99            }
100            partitions[spi.partitionId] = partition;
101        }
102        return partitions;
103    }
104
105    public Connection getConnection() {
106        return connection;
107    }
108
109    void setConnection(final Connection connection) {
110        this.connection = connection;
111    }
112
113    public void readData(DataInput in) throws IOException {
114        masterTime = in.readLong();
115        version = in.readInt();
116        int size = in.readInt();
117        final Map<Address, Integer> addressIndexes = new HashMap<Address, Integer>(size);
118        int memberIndex = 0;
119        while (size-- > 0) {
120            MemberInfo memberInfo = new MemberInfo();
121            memberInfo.readData(in);
122            addMemberInfo(memberInfo, addressIndexes, memberIndex);
123            memberIndex++;
124        }
125        int partitionCount = in.readInt();
126        for (int i = 0; i < partitionCount; i++) {
127            ShortPartitionInfo spi = new ShortPartitionInfo();
128            spi.readData(in);
129            partitionInfos.add(spi);
130        }
131    }
132
133    public void writeData(DataOutput out) throws IOException {
134        out.writeLong(masterTime);
135        out.writeInt(version);
136        int memberSize = members.size();
137        out.writeInt(memberSize);
138        for (int i = 0; i < memberSize; i++) {
139            MemberInfo memberInfo = members.get(i);
140            memberInfo.writeData(out);
141        }
142        out.writeInt(partitionInfos.size());
143        for (ShortPartitionInfo spi : partitionInfos) {
144            spi.writeData(out);
145        }
146    }
147
148    @Override
149    public String toString() {
150        StringBuilder sb = new StringBuilder("PartitionRuntimeState [" + version + "]{\n");
151        for (MemberInfo address : members) {
152            sb.append(address).append('\n');
153        }
154        sb.append('}');
155        return sb.toString();
156    }
157
158    public int getVersion() {
159        return version;
160    }
161
162    class ShortPartitionInfo implements DataSerializable {
163
164        int partitionId;
165        int[] addressIndexes = new int[PartitionInfo.MAX_REPLICA_COUNT];
166
167        ShortPartitionInfo(int partitionId) {
168            this.partitionId = partitionId;
169        }
170
171        ShortPartitionInfo() {
172        }
173
174        public void writeData(DataOutput out) throws IOException {
175            out.writeInt(partitionId);
176            for (int i = 0; i < PartitionInfo.MAX_REPLICA_COUNT; i++) {
177                out.writeInt(addressIndexes[i]);
178            }
179        }
180
181        public void readData(DataInput in) throws IOException {
182            partitionId = in.readInt();
183            for (int i = 0; i < PartitionInfo.MAX_REPLICA_COUNT; i++) {
184                addressIndexes[i] = in.readInt();
185            }
186        }
187    }
188}