/hazelcast/src/main/java/com/hazelcast/impl/PartitionServiceImpl.java

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 213 lines · 174 code · 24 blank · 15 comment · 36 complexity · 511763006b41413d21d7c6e694928aa1 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;
  17. import com.hazelcast.core.Member;
  18. import com.hazelcast.impl.partition.MigrationStatus;
  19. import com.hazelcast.logging.ILogger;
  20. import com.hazelcast.nio.Address;
  21. import com.hazelcast.nio.Data;
  22. import com.hazelcast.partition.MigrationEvent;
  23. import com.hazelcast.partition.MigrationListener;
  24. import com.hazelcast.partition.Partition;
  25. import com.hazelcast.partition.PartitionService;
  26. import com.hazelcast.util.ResponseQueueFactory;
  27. import java.util.List;
  28. import java.util.Set;
  29. import java.util.TreeSet;
  30. import java.util.concurrent.*;
  31. import java.util.logging.Level;
  32. import static com.hazelcast.nio.IOUtil.toData;
  33. public class PartitionServiceImpl implements PartitionService {
  34. private final ILogger logger;
  35. private final ConcurrentMap<Integer, PartitionProxy> mapPartitions = new ConcurrentHashMap<Integer, PartitionProxy>();
  36. private final List<MigrationListener> lsMigrationListeners = new CopyOnWriteArrayList<MigrationListener>();
  37. private final ConcurrentMapManager concurrentMapManager;
  38. private final Set<Partition> partitions;
  39. public PartitionServiceImpl(ConcurrentMapManager concurrentMapManager) {
  40. this.logger = concurrentMapManager.node.getLogger(PartitionService.class.getName());
  41. this.concurrentMapManager = concurrentMapManager;
  42. this.partitions = new TreeSet<Partition>();
  43. for (int i = 0; i < concurrentMapManager.partitionCount; i++) {
  44. PartitionProxy partitionProxy = new PartitionProxy(i);
  45. partitions.add(partitionProxy);
  46. mapPartitions.put(i, partitionProxy);
  47. }
  48. }
  49. public int getOwnedPartitionCount() {
  50. int currentCount = 0;
  51. for (Partition partition : partitions) {
  52. if (partition.getOwner() == null || partition.getOwner().localMember()) {
  53. currentCount++;
  54. }
  55. }
  56. return currentCount;
  57. }
  58. public Set<Partition> getPartitions() {
  59. return partitions;
  60. }
  61. public PartitionProxy getPartition(Object key) {
  62. final Data keyData = toData(key);
  63. final int partitionId = concurrentMapManager.getPartitionId(keyData);
  64. return getPartition(partitionId);
  65. }
  66. public PartitionProxy getPartition(int partitionId) {
  67. return mapPartitions.get(partitionId);
  68. }
  69. void doFireMigrationEvent(final MigrationStatus status, final MigrationEvent migrationEvent) {
  70. if (migrationEvent == null) throw new IllegalArgumentException("MigrationEvent is null.");
  71. if (status == MigrationStatus.COMPLETED) {
  72. concurrentMapManager.node.executorManager.executeNow(new Runnable() {
  73. public void run() {
  74. if (migrationEvent.getOldOwner() != null && migrationEvent.getOldOwner().localMember()) {
  75. for (CMap cMap : concurrentMapManager.maps.values()) {
  76. for (Record record : cMap.getMapIndexService().getOwnedRecords()) {
  77. if (record.getBlockId() == migrationEvent.getPartitionId()) {
  78. cMap.getMapIndexService().remove(record);
  79. }
  80. }
  81. }
  82. }
  83. }
  84. });
  85. }
  86. for (final MigrationListener migrationListener : lsMigrationListeners) {
  87. concurrentMapManager.node.executorManager.executeNow(new Runnable() {
  88. public void run() {
  89. switch (status) {
  90. case STARTED:
  91. migrationListener.migrationStarted(migrationEvent);
  92. break;
  93. case COMPLETED:
  94. migrationListener.migrationCompleted(migrationEvent);
  95. break;
  96. case FAILED:
  97. migrationListener.migrationFailed(migrationEvent);
  98. break;
  99. }
  100. }
  101. });
  102. }
  103. }
  104. public void addMigrationListener(MigrationListener migrationListener) {
  105. lsMigrationListeners.add(migrationListener);
  106. }
  107. public void removeMigrationListener(MigrationListener migrationListener) {
  108. lsMigrationListeners.remove(migrationListener);
  109. }
  110. public void reset() {
  111. }
  112. boolean allPartitionsOwned() {
  113. Set<Partition> partitions = getPartitions();
  114. for (Partition partition : partitions) {
  115. if (partition.getOwner() == null) {
  116. return false;
  117. }
  118. }
  119. return true;
  120. }
  121. private MemberImpl getPartitionOwner(final int partitionId) throws InterruptedException {
  122. final BlockingQueue<MemberImpl> responseQ = ResponseQueueFactory.newResponseQueue();
  123. concurrentMapManager.enqueueAndReturn(new Processable() {
  124. public void process() {
  125. MemberImpl memberOwner = null;
  126. try {
  127. Address ownerAddress = concurrentMapManager.getPartitionManager().getOwner(partitionId);
  128. if (ownerAddress != null) {
  129. if (concurrentMapManager.thisAddress.equals(ownerAddress)) {
  130. memberOwner = concurrentMapManager.thisMember;
  131. } else {
  132. memberOwner = concurrentMapManager.getMember(ownerAddress);
  133. }
  134. }
  135. } catch (Exception e) {
  136. logger.log(Level.SEVERE, e.getMessage(), e);
  137. } finally {
  138. responseQ.offer(memberOwner);
  139. }
  140. }
  141. });
  142. return responseQ.poll(10, TimeUnit.SECONDS);
  143. }
  144. class PartitionProxy implements Partition, Comparable {
  145. final int partitionId;
  146. PartitionProxy(int partitionId) {
  147. this.partitionId = partitionId;
  148. }
  149. public int getPartitionId() {
  150. return partitionId;
  151. }
  152. public Member getOwner() {
  153. Address address = concurrentMapManager.getPartitionManager().getPartition(partitionId).getOwner();
  154. if (address != null) {
  155. Member member = concurrentMapManager.node.getClusterImpl().getMember(address);
  156. if (member != null) {
  157. return member;
  158. }
  159. }
  160. try {
  161. return getPartitionOwner(partitionId);
  162. } catch (InterruptedException e) {
  163. return null;
  164. }
  165. }
  166. public int compareTo(Object o) {
  167. PartitionProxy partition = (PartitionProxy) o;
  168. Integer id = partitionId;
  169. return (id.compareTo(partition.getPartitionId()));
  170. }
  171. @Override
  172. public boolean equals(Object o) {
  173. if (this == o) return true;
  174. if (o == null || getClass() != o.getClass()) return false;
  175. PartitionProxy partition = (PartitionProxy) o;
  176. return partitionId == partition.partitionId;
  177. }
  178. @Override
  179. public int hashCode() {
  180. return partitionId;
  181. }
  182. @Override
  183. public String toString() {
  184. return "Partition [" +
  185. +partitionId +
  186. "], owner=" + getOwner();
  187. }
  188. }
  189. }