PageRenderTime 71ms CodeModel.GetById 29ms app.highlight 37ms RepoModel.GetById 1ms app.codeStats 0ms

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