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

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 708 lines · 565 code · 128 blank · 15 comment · 66 complexity · cdf53d3a87797fee377c517796bff532 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.config.Config;
  18. import com.hazelcast.core.*;
  19. import com.hazelcast.impl.partition.PartitionInfo;
  20. import com.hazelcast.impl.partition.PartitionListener;
  21. import com.hazelcast.impl.partition.PartitionReplicaChangeEvent;
  22. import com.hazelcast.nio.Address;
  23. import com.hazelcast.nio.Data;
  24. import com.hazelcast.partition.MigrationEvent;
  25. import com.hazelcast.partition.MigrationListener;
  26. import com.hazelcast.partition.Partition;
  27. import com.hazelcast.partition.PartitionService;
  28. import org.junit.Ignore;
  29. import java.io.Serializable;
  30. import java.sql.Timestamp;
  31. import java.util.Date;
  32. import java.util.concurrent.Callable;
  33. import java.util.concurrent.CountDownLatch;
  34. import java.util.concurrent.TimeUnit;
  35. import static com.hazelcast.nio.IOUtil.toData;
  36. import static junit.framework.Assert.assertEquals;
  37. import static junit.framework.Assert.assertTrue;
  38. import static org.mockito.Mockito.mock;
  39. @Ignore
  40. public class TestUtil {
  41. public static boolean migrateKey(Object key, HazelcastInstance oldest, HazelcastInstance to, final int replicaIndex) throws Exception {
  42. final int partitionId = oldest.getPartitionService().getPartition(key).getPartitionId();
  43. final ConcurrentMapManager concurrentMapManagerOldest = getConcurrentMapManager(oldest);
  44. final ConcurrentMapManager concurrentMapManagerTo = getConcurrentMapManager(to);
  45. final PartitionInfo partitionInfoOldest = concurrentMapManagerOldest.getPartitionInfo(partitionId);
  46. final PartitionInfo partitionInfoTo = concurrentMapManagerTo.getPartitionInfo(partitionId);
  47. final MemberImpl currentOwnerMember = concurrentMapManagerOldest.getMember(partitionInfoOldest.getReplicaAddress(replicaIndex));
  48. final MemberImpl toMember = (MemberImpl) to.getCluster().getLocalMember();
  49. if (!currentOwnerMember.equals(toMember)) {
  50. final Address addressCurrentOwner = currentOwnerMember.getAddress();
  51. final Address addressNewOwner = toMember.getAddress();
  52. PartitionListenerLatch latchOldest = new PartitionListenerLatch(toMember.getAddress(), partitionId, replicaIndex);
  53. PartitionListenerLatch latchTo = new PartitionListenerLatch(toMember.getAddress(), partitionId, replicaIndex);
  54. concurrentMapManagerOldest.getPartitionManager().addPartitionListener(latchOldest);
  55. concurrentMapManagerTo.getPartitionManager().addPartitionListener(latchTo);
  56. concurrentMapManagerOldest.enqueueAndReturn(new Processable() {
  57. public void process() {
  58. concurrentMapManagerOldest.partitionManager.forcePartitionOwnerMigration(partitionId, replicaIndex, addressCurrentOwner, addressNewOwner);
  59. }
  60. });
  61. assertTrue("Migration should get completed in 20 seconds!!", latchOldest.await(20, TimeUnit.SECONDS));
  62. assertTrue("Migration should get completed in 20 seconds!!", latchTo.await(20, TimeUnit.SECONDS));
  63. }
  64. assertEquals(toMember.getAddress(), partitionInfoOldest.getReplicaAddress(replicaIndex));
  65. assertEquals(toMember.getAddress(), partitionInfoTo.getReplicaAddress(replicaIndex));
  66. return true;
  67. }
  68. public static class MigrationCompletionLatch implements MigrationListener {
  69. final int partitionId;
  70. final CountDownLatch latch;
  71. public MigrationCompletionLatch(Object key, HazelcastInstance... h) {
  72. this.partitionId = h[0].getPartitionService().getPartition(key).getPartitionId();
  73. this.latch = new CountDownLatch(h.length);
  74. for (HazelcastInstance hazelcastInstance : h) {
  75. hazelcastInstance.getPartitionService().addMigrationListener(this);
  76. }
  77. }
  78. public void migrationStarted(MigrationEvent migrationEvent) {
  79. }
  80. public void migrationCompleted(MigrationEvent migrationEvent) {
  81. if (migrationEvent.getPartitionId() == partitionId) {
  82. latch.countDown();
  83. }
  84. }
  85. public void migrationFailed(final MigrationEvent migrationEvent) {
  86. }
  87. public boolean await(int time, TimeUnit timeUnit) throws InterruptedException {
  88. return latch.await(time, timeUnit);
  89. }
  90. }
  91. static class PartitionListenerLatch implements PartitionListener {
  92. final CountDownLatch migrationLatch = new CountDownLatch(1);
  93. final Address toAddress;
  94. final int partitionId;
  95. final int replicaIndex;
  96. PartitionListenerLatch(Address toAddress, int partitionId, int replicaIndex) {
  97. this.toAddress = toAddress;
  98. this.partitionId = partitionId;
  99. this.replicaIndex = replicaIndex;
  100. }
  101. public void replicaChanged(PartitionReplicaChangeEvent event) {
  102. if (event.getReplicaIndex() == replicaIndex
  103. && event.getPartitionId() == partitionId
  104. && toAddress != null
  105. && toAddress.equals(event.getNewAddress())) {
  106. migrationLatch.countDown();
  107. }
  108. }
  109. public boolean await(int time, TimeUnit timeUnit) throws InterruptedException {
  110. return migrationLatch.await(time, timeUnit);
  111. }
  112. }
  113. public static Node getNode(HazelcastInstance h) {
  114. FactoryImpl.HazelcastInstanceProxy hiProxy = (FactoryImpl.HazelcastInstanceProxy) h;
  115. return hiProxy.getFactory().node;
  116. }
  117. public static ConcurrentMapManager getConcurrentMapManager(HazelcastInstance h) {
  118. return getNode(h).concurrentMapManager;
  119. }
  120. public static CMap mockCMap(String name) {
  121. FactoryImpl mockFactory = mock(FactoryImpl.class);
  122. Node node = new Node(mockFactory, new Config());
  123. node.serviceThread = Thread.currentThread();
  124. return new CMap(node.concurrentMapManager, "c:" + name);
  125. }
  126. public static CMap getCMap(HazelcastInstance h, String name) {
  127. ConcurrentMapManager concurrentMapManager = getConcurrentMapManager(h);
  128. String fullName = Prefix.MAP + name;
  129. return concurrentMapManager.getMap(fullName);
  130. }
  131. public static CMap getCMapForMultiMap(HazelcastInstance h, String name) {
  132. ConcurrentMapManager concurrentMapManager = getConcurrentMapManager(h);
  133. String fullName = Prefix.MULTIMAP + name;
  134. return concurrentMapManager.getMap(fullName);
  135. }
  136. public static Partition getPartitionById(PartitionService partitionService, int partitionId) {
  137. for (Partition partition : partitionService.getPartitions()) {
  138. if (partition.getPartitionId() == partitionId) {
  139. return partition;
  140. }
  141. }
  142. return null;
  143. }
  144. public static Record newRecord(CMap cmap, long recordId, Data key, Data value) {
  145. return new DefaultRecord(cmap, 1, key, value, 0, 0, recordId);
  146. }
  147. public static Record newRecord(long recordId, Data key, Data value) {
  148. CMap cmap = mock(CMap.class);
  149. return newRecord(cmap, recordId, key, value);
  150. }
  151. public static Record newRecord(CMap cmap, long recordId, Object key, Object value) {
  152. return newRecord(cmap, recordId, toData(key), toData(value));
  153. }
  154. public static Record newRecord(long recordId, Object key, Object value) {
  155. return newRecord(recordId, toData(key), toData(value));
  156. }
  157. public static Record newRecord(long recordId) {
  158. return newRecord(recordId, null, null);
  159. }
  160. public static Request newPutRequest(Data key, Data value) {
  161. return newPutRequest(key, value, -1);
  162. }
  163. public static Request newPutRequest(Data key, Data value, long ttl) {
  164. return newRequest(ClusterOperation.CONCURRENT_MAP_PUT, key, value, ttl);
  165. }
  166. public static Request newPutIfAbsentRequest(Data key, Data value, long ttl) {
  167. return newRequest(ClusterOperation.CONCURRENT_MAP_PUT_IF_ABSENT, key, value, ttl);
  168. }
  169. public static Request newRequest(ClusterOperation operation, Data key, Data value, long ttl) {
  170. Request request = new Request();
  171. request.setLocal(operation, null, key, value, -1, -1, ttl, null);
  172. return request;
  173. }
  174. public static Request newRemoveRequest(Data key) {
  175. return newRequest(ClusterOperation.CONCURRENT_MAP_REMOVE, key, null, -1);
  176. }
  177. public static Request newEvictRequest(Data key) {
  178. return newRequest(ClusterOperation.CONCURRENT_MAP_EVICT, key, null, -1);
  179. }
  180. public static Request newGetRequest(Data key) {
  181. return newRequest(ClusterOperation.CONCURRENT_MAP_GET, key, null, -1);
  182. }
  183. public static Request newContainsRequest(Data key, Data value) {
  184. if (value == null) {
  185. return newRequest(ClusterOperation.CONCURRENT_MAP_CONTAINS_KEY, key, value, -1);
  186. } else {
  187. return newRequest(ClusterOperation.CONCURRENT_MAP_CONTAINS_ENTRY, key, value, -1);
  188. }
  189. }
  190. @Ignore
  191. public static class ValueType implements Serializable {
  192. String typeName;
  193. public ValueType(String typeName) {
  194. this.typeName = typeName;
  195. }
  196. public ValueType() {
  197. }
  198. public String getTypeName() {
  199. return typeName;
  200. }
  201. }
  202. @Ignore
  203. public static abstract class AbstractValue implements Serializable {
  204. public String name;
  205. public AbstractValue(String name) {
  206. this.name = name;
  207. }
  208. protected AbstractValue() {
  209. }
  210. @Override
  211. public boolean equals(final Object o) {
  212. if (this == o) return true;
  213. if (o == null || getClass() != o.getClass()) return false;
  214. final AbstractValue that = (AbstractValue) o;
  215. if (name != null ? !name.equals(that.name) : that.name != null) return false;
  216. return true;
  217. }
  218. @Override
  219. public int hashCode() {
  220. return name != null ? name.hashCode() : 0;
  221. }
  222. }
  223. @Ignore
  224. public static class Value extends AbstractValue implements Serializable {
  225. ValueType type;
  226. int index;
  227. public Value(String name, ValueType type, int index) {
  228. super(name);
  229. this.type = type;
  230. this.index = index;
  231. }
  232. public Value(String name, int index) {
  233. super(name);
  234. this.index = index;
  235. }
  236. public Value(String name) {
  237. this(name, null, 0);
  238. }
  239. public Value() {
  240. super("unknown");
  241. }
  242. public ValueType getType() {
  243. return type;
  244. }
  245. public int getIndex() {
  246. return index;
  247. }
  248. public void setIndex(final int index) {
  249. this.index = index;
  250. }
  251. @Override
  252. public boolean equals(final Object o) {
  253. if (this == o) return true;
  254. if (o == null || getClass() != o.getClass()) return false;
  255. if (!super.equals(o)) return false;
  256. final Value value = (Value) o;
  257. if (index != value.index) return false;
  258. if (type != null ? !type.equals(value.type) : value.type != null) return false;
  259. return true;
  260. }
  261. @Override
  262. public int hashCode() {
  263. int result = super.hashCode();
  264. result = 31 * result + (type != null ? type.hashCode() : 0);
  265. result = 31 * result + index;
  266. return result;
  267. }
  268. @Override
  269. public String toString() {
  270. final StringBuilder sb = new StringBuilder();
  271. sb.append("Value");
  272. sb.append("{name=").append(name);
  273. sb.append(", index=").append(index);
  274. sb.append(", type=").append(type);
  275. sb.append('}');
  276. return sb.toString();
  277. }
  278. }
  279. @Ignore
  280. public static class EmptyMapEntry implements MapEntry {
  281. private long cost;
  282. private long creationTime;
  283. private long expirationTime;
  284. private int hits;
  285. private long lastAccessTime;
  286. private long lastUpdateTime;
  287. private long lastStoredTime;
  288. private int version;
  289. private boolean valid;
  290. private Object key;
  291. private Object value;
  292. private long id;
  293. public EmptyMapEntry(long id) {
  294. this.id = id;
  295. }
  296. public long getCost() {
  297. return cost;
  298. }
  299. public long getCreationTime() {
  300. return creationTime;
  301. }
  302. public long getExpirationTime() {
  303. return expirationTime;
  304. }
  305. public int getHits() {
  306. return hits;
  307. }
  308. public long getLastAccessTime() {
  309. return lastAccessTime;
  310. }
  311. public long getLastStoredTime() {
  312. return lastStoredTime;
  313. }
  314. public long getLastUpdateTime() {
  315. return lastUpdateTime;
  316. }
  317. public long getVersion() {
  318. return version;
  319. }
  320. public boolean isValid() {
  321. return valid;
  322. }
  323. public Object getKey() {
  324. return key;
  325. }
  326. public Object getValue() {
  327. return value;
  328. }
  329. public Object setValue(Object value) {
  330. Object oldValue = this.value;
  331. this.value = value;
  332. return oldValue;
  333. }
  334. public void setCost(long cost) {
  335. this.cost = cost;
  336. }
  337. public void setCreationTime(long creationTime) {
  338. this.creationTime = creationTime;
  339. }
  340. public void setExpirationTime(long expirationTime) {
  341. this.expirationTime = expirationTime;
  342. }
  343. public void setHits(int hits) {
  344. this.hits = hits;
  345. }
  346. public void setKey(Object key) {
  347. this.key = key;
  348. }
  349. public void setLastAccessTime(long lastAccessTime) {
  350. this.lastAccessTime = lastAccessTime;
  351. }
  352. public void setLastUpdateTime(long lastUpdateTime) {
  353. this.lastUpdateTime = lastUpdateTime;
  354. }
  355. public void setValid(boolean valid) {
  356. this.valid = valid;
  357. }
  358. public void setVersion(int version) {
  359. this.version = version;
  360. }
  361. public long getId() {
  362. return id;
  363. }
  364. @Override
  365. public boolean equals(Object o) {
  366. if (this == o) return true;
  367. if (o == null || getClass() != o.getClass()) return false;
  368. EmptyMapEntry that = (EmptyMapEntry) o;
  369. if (id != that.id) return false;
  370. return true;
  371. }
  372. @Override
  373. public int hashCode() {
  374. return (int) (id ^ (id >>> 32));
  375. }
  376. @Override
  377. public String toString() {
  378. return "EmptyMapEntry{" +
  379. "id=" + id +
  380. ", expirationTime=" + expirationTime +
  381. ", hits=" + hits +
  382. ", lastAccessTime=" + lastAccessTime +
  383. ", lastUpdateTime=" + lastUpdateTime +
  384. ", key=" + key +
  385. ", value=" + value +
  386. ", valid=" + valid +
  387. ", creationTime=" + creationTime +
  388. ", cost=" + cost +
  389. ", version=" + version +
  390. '}';
  391. }
  392. }
  393. @Ignore
  394. public static class OrderUpdateRunnable implements Serializable, Runnable, PartitionAware<Integer>, HazelcastInstanceAware {
  395. int customerId;
  396. int orderId;
  397. transient HazelcastInstance hazelcastInstance;
  398. public OrderUpdateRunnable(int orderId, int customerId) {
  399. this.customerId = customerId;
  400. this.orderId = orderId;
  401. }
  402. public void run() {
  403. if (!hazelcastInstance.getPartitionService().getPartition(customerId).getOwner().localMember()) {
  404. throw new RuntimeException("Not local member");
  405. }
  406. }
  407. public int getCustomerId() {
  408. return customerId;
  409. }
  410. public int getOrderId() {
  411. return orderId;
  412. }
  413. public Integer getPartitionKey() {
  414. return customerId;
  415. }
  416. public void setHazelcastInstance(HazelcastInstance hazelcastInstance) {
  417. this.hazelcastInstance = hazelcastInstance;
  418. }
  419. @Override
  420. public String toString() {
  421. return "OrderUpdateRunnable{" +
  422. "customerId=" + customerId +
  423. ", orderId=" + orderId +
  424. '}';
  425. }
  426. }
  427. @Ignore
  428. public static class OrderUpdateCallable implements Serializable, Callable<Boolean>, PartitionAware, HazelcastInstanceAware {
  429. int customerId;
  430. int orderId;
  431. transient HazelcastInstance hazelcastInstance;
  432. public OrderUpdateCallable(int orderId, int customerId) {
  433. this.customerId = customerId;
  434. this.orderId = orderId;
  435. }
  436. public Boolean call() throws Exception {
  437. return hazelcastInstance.getPartitionService().getPartition(customerId).getOwner().localMember();
  438. }
  439. public int getCustomerId() {
  440. return customerId;
  441. }
  442. public int getOrderId() {
  443. return orderId;
  444. }
  445. public Object getPartitionKey() {
  446. return customerId;
  447. }
  448. public void setHazelcastInstance(HazelcastInstance hazelcastInstance) {
  449. this.hazelcastInstance = hazelcastInstance;
  450. }
  451. @Override
  452. public String toString() {
  453. return "OrderUpdateCallable{" +
  454. "customerId=" + customerId +
  455. ", orderId=" + orderId +
  456. '}';
  457. }
  458. }
  459. @Ignore
  460. public static class OrderKey implements Serializable, PartitionAware {
  461. int customerId;
  462. int orderId;
  463. public OrderKey(int orderId, int customerId) {
  464. this.customerId = customerId;
  465. this.orderId = orderId;
  466. }
  467. public int getCustomerId() {
  468. return customerId;
  469. }
  470. public int getOrderId() {
  471. return orderId;
  472. }
  473. public Object getPartitionKey() {
  474. return customerId;
  475. }
  476. @Override
  477. public String toString() {
  478. return "OrderKey{" +
  479. "customerId=" + customerId +
  480. ", orderId=" + orderId +
  481. '}';
  482. }
  483. }
  484. public static enum State {
  485. STATE1, STATE2
  486. }
  487. @Ignore
  488. public static class Employee implements Serializable {
  489. long id;
  490. String name;
  491. String city;
  492. int age;
  493. boolean active;
  494. double salary;
  495. Timestamp date;
  496. Date createDate;
  497. java.sql.Date sqlDate;
  498. State state;
  499. public Employee(long id, String name, int age, boolean live, double salary, State state) {
  500. this.state = state;
  501. }
  502. public Employee(long id, String name, int age, boolean live, double salary) {
  503. this(id, name, null, age, live, salary);
  504. }
  505. public Employee(String name, int age, boolean live, double salary) {
  506. this(-1, name, age, live, salary);
  507. }
  508. public Employee(long id, String name, String city, int age, boolean live, double salary) {
  509. this.id = id;
  510. this.name = name;
  511. this.city = city;
  512. this.age = age;
  513. this.active = live;
  514. this.salary = salary;
  515. this.createDate = new Date();
  516. this.date = new Timestamp(createDate.getTime());
  517. this.sqlDate = new java.sql.Date(createDate.getTime());
  518. }
  519. public Employee() {
  520. }
  521. public Timestamp getDate() {
  522. return date;
  523. }
  524. public String getName() {
  525. return name;
  526. }
  527. public String getCity() {
  528. return city;
  529. }
  530. public int getAge() {
  531. return age;
  532. }
  533. public double getSalary() {
  534. return salary;
  535. }
  536. public boolean isActive() {
  537. return active;
  538. }
  539. public State getState() {
  540. return state;
  541. }
  542. public void setState(State state) {
  543. this.state = state;
  544. }
  545. @Override
  546. public boolean equals(Object o) {
  547. if (this == o) return true;
  548. if (o == null || getClass() != o.getClass()) return false;
  549. Employee employee = (Employee) o;
  550. if (active != employee.active) return false;
  551. if (age != employee.age) return false;
  552. if (Double.compare(employee.salary, salary) != 0) return false;
  553. if (name != null ? !name.equals(employee.name) : employee.name != null) return false;
  554. return true;
  555. }
  556. @Override
  557. public int hashCode() {
  558. int result;
  559. long temp;
  560. result = name != null ? name.hashCode() : 0;
  561. result = 31 * result + age;
  562. result = 31 * result + (active ? 1 : 0);
  563. temp = salary != +0.0d ? Double.doubleToLongBits(salary) : 0L;
  564. result = 31 * result + (int) (temp ^ (temp >>> 32));
  565. return result;
  566. }
  567. @Override
  568. public String toString() {
  569. final StringBuffer sb = new StringBuffer();
  570. sb.append("Employee");
  571. sb.append("{name='").append(name).append('\'');
  572. sb.append(", city=").append(city);
  573. sb.append(", age=").append(age);
  574. sb.append(", active=").append(active);
  575. sb.append(", salary=").append(salary);
  576. sb.append('}');
  577. return sb.toString();
  578. }
  579. }
  580. }