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