@Test public void confirmLocationRunEntryProcessor() { withHazelcastInstance(3, hazelcast -> { IMap<String, String> map = hazelcast.getMap("default"); IntStream.rangeClosed(1, 10).forEach(i -> map.put("key" + i, "value" + i)); PartitionService ps = hazelcast.getPartitionService(); IntStream .rangeClosed(1, 10) .forEach(i -> { String key = "key" + i; System.out.printf("key = %s, location = %s%n", key, ps.getPartition(key).getOwner().getUuid()); assertThat( map.executeOnKey(key, new ReturnLocationEntryProcessor()) ).isEqualTo(ps.getPartition(key).getOwner().getUuid()); }); }); }
public static void warmupPartitions(HazelcastInstance hazelcastInstance) { LOGGER.info("Waiting for partition warmup"); PartitionService partitionService = hazelcastInstance.getPartitionService(); long started = System.nanoTime(); for (Partition partition : partitionService.getPartitions()) { if (System.nanoTime() - started > PARTITION_WARMUP_TIMEOUT_NANOS) { throw new IllegalStateException("Partition warmup timeout. Partitions didn't get an owner in time"); } while (partition.getOwner() == null) { LOGGER.debug("Partition owner is not yet set for partitionId: " + partition.getPartitionId()); sleepMillisThrowException(PARTITION_WARMUP_SLEEP_INTERVAL_MILLIS); } } LOGGER.info("Partitions are warmed up successfully"); }
@Test public void indexingTest() { List<Book> books = Arrays.asList( new Book("978-4774169316", "Javaエンジニア養成読本", 2138), new Book("978-4798124605", "Beginning Java EE 6 GlassFish 3で始めるエンタープライズJava", 4536), new Book("978-4873117188", "Javaパフォーマンス", 4212) ); withHazelcast(3, hazelcast -> { IMap<String, Book> map = hazelcast.getMap("default"); books.stream().forEach(b -> map.put(b.getIsbn(), b)); try { System.out.println("Sleeping..."); TimeUnit.SECONDS.sleep(10L); } catch (InterruptedException e) { } PartitionService ps = hazelcast.getPartitionService(); System.out.printf( "%s:%s => %s%n", "978-4774169316", "Javaエンジニア養成読本", ps.getPartition("978-4774169316").getOwner() ); System.out.printf( "%s:%s => %s%n", "978-4798124605", "Beginning Java EE 6 GlassFish 3で始めるエンタープライズJava", ps.getPartition("978-4798124605").getOwner() ); System.out.printf( "%s:%s => %s%n", "978-4873117188", "Javaパフォーマンス", ps.getPartition("978-4873117188").getOwner() ); }); }
@Override public Object process(Map.Entry<String, String> entry) { PartitionService ps = hazelcast.getPartitionService(); System.out.printf( "key = %s, run entry processor member = %s, owner? = %b, locked? = %b%n", entry.getKey(), hazelcast.getCluster().getLocalMember().getUuid(), ps.getPartition(entry.getKey()).getOwner().getUuid().equals(hazelcast.getCluster().getLocalMember().getUuid()), ((LockAware) entry).isLocked() ); return hazelcast.getCluster().getLocalMember().getUuid(); }
/** * Returns the next {@code long} key owned by the given Hazelcast instance. * * @param instance Hazelcast instance to search next key for * @param lastKey last key to start search from * @return next key owned by given Hazelcast instance */ public static long nextKeyOwnedBy(HazelcastInstance instance, long lastKey) { Member localMember = instance.getCluster().getLocalMember(); PartitionService partitionService = instance.getPartitionService(); while (true) { Partition partition = partitionService.getPartition(lastKey); if (localMember.equals(partition.getOwner())) { return lastKey; } lastKey++; } }
/** * Checks if a key is located on a Hazelcast instance. * * @param instance the HazelcastInstance the key should belong to * @param key the key to check * @return <tt>true</tt> if the key belongs to the Hazelcast instance, <tt>false</tt> otherwise */ public static boolean isLocalKey(HazelcastInstance instance, Object key) { PartitionService partitionService = instance.getPartitionService(); Partition partition = partitionService.getPartition(key); Member owner; while (true) { owner = partition.getOwner(); if (owner != null) { break; } sleepSeconds(1); } return owner.equals(instance.getLocalEndpoint()); }
@Override public PartitionService getPartitionService() { return null; }
public PartitionPredicate(PartitionService partService, Set<Integer> partitions, Predicate<K, V> predicate) { this.partService = partService; this.partitions = partitions; this.predicate = predicate; }
public MemberPredicate(PartitionService partService, Member member, Predicate<K, V> predicate) { this.partService = partService; this.member = member; this.predicate = predicate; }
@Test public void test_simple_backup_create_sequencer_definition_owner() { TestHazelcastInstanceFactory factory = new TestHazelcastInstanceFactory(2); HazelcastInstance hazelcastInstance1 = factory.newHazelcastInstance(); HazelcastInstance hazelcastInstance2 = factory.newHazelcastInstance(); try { final String sequencerName = generateKeyOwnedBy(hazelcastInstance1); // Build the custom epoch SnowcastEpoch epoch = buildEpoch(); Snowcast snowcast1 = SnowcastSystem.snowcast(hazelcastInstance1); Snowcast snowcast2 = SnowcastSystem.snowcast(hazelcastInstance2); InternalSequencer sequencer1 = (InternalSequencer) buildSnowcastSequencer(snowcast1, sequencerName, epoch); InternalSequencer sequencer2 = (InternalSequencer) buildSnowcastSequencer(snowcast2, sequencerName, epoch); NodeSequencerService sequencerService1 = (NodeSequencerService) sequencer1.getSequencerService(); NodeSequencerService sequencerService2 = (NodeSequencerService) sequencer2.getSequencerService(); PartitionService partitionService = hazelcastInstance1.getPartitionService(); int partitionId = partitionService.getPartition(sequencerName).getPartitionId(); final SequencerPartition partition1 = sequencerService1.getSequencerPartition(partitionId); final SequencerPartition partition2 = sequencerService2.getSequencerPartition(partitionId); assertTrueEventually(new AssertTask() { @Override public void run() throws Exception { SequencerDefinition sequencerDefinition1 = partition1.getSequencerDefinition(sequencerName); SequencerDefinition sequencerDefinition2 = partition2.getSequencerDefinition(sequencerName); assertEquals(sequencerDefinition1, sequencerDefinition2); } }); } finally { factory.shutdownAll(); } }
@Test public void test_simple_backup_create_sequencer_definition_non_owner() { TestHazelcastInstanceFactory factory = new TestHazelcastInstanceFactory(2); HazelcastInstance hazelcastInstance1 = factory.newHazelcastInstance(); HazelcastInstance hazelcastInstance2 = factory.newHazelcastInstance(); try { final String sequencerName = generateKeyOwnedBy(hazelcastInstance1); // Build the custom epoch SnowcastEpoch epoch = buildEpoch(); Snowcast snowcast1 = SnowcastSystem.snowcast(hazelcastInstance1); Snowcast snowcast2 = SnowcastSystem.snowcast(hazelcastInstance2); InternalSequencer sequencer2 = (InternalSequencer) buildSnowcastSequencer(snowcast2, sequencerName, epoch); InternalSequencer sequencer1 = (InternalSequencer) buildSnowcastSequencer(snowcast1, sequencerName, epoch); NodeSequencerService sequencerService1 = (NodeSequencerService) sequencer1.getSequencerService(); NodeSequencerService sequencerService2 = (NodeSequencerService) sequencer2.getSequencerService(); PartitionService partitionService = hazelcastInstance1.getPartitionService(); int partitionId = partitionService.getPartition(sequencerName).getPartitionId(); final SequencerPartition partition1 = sequencerService1.getSequencerPartition(partitionId); final SequencerPartition partition2 = sequencerService2.getSequencerPartition(partitionId); assertTrueEventually(new AssertTask() { @Override public void run() throws Exception { SequencerDefinition sequencerDefinition1 = partition1.getSequencerDefinition(sequencerName); SequencerDefinition sequencerDefinition2 = partition2.getSequencerDefinition(sequencerName); assertEquals(sequencerDefinition1, sequencerDefinition2); } }); } finally { factory.shutdownAll(); } }
@Test public void test_simple_backup_create_sequencer_definition_client() { HazelcastInstance hazelcastInstance1 = Hazelcast.newHazelcastInstance(config1); HazelcastInstance hazelcastInstance2 = Hazelcast.newHazelcastInstance(config2); HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig); try { final String sequencerName = generateKeyOwnedBy(hazelcastInstance1); // Build the custom epoch SnowcastEpoch epoch = buildEpoch(); Snowcast clientSnowcast = SnowcastSystem.snowcast(client); Snowcast snowcast1 = SnowcastSystem.snowcast(hazelcastInstance1); Snowcast snowcast2 = SnowcastSystem.snowcast(hazelcastInstance2); buildSnowcastSequencer(clientSnowcast, sequencerName, epoch); InternalSequencer sequencer1 = (InternalSequencer) buildSnowcastSequencer(snowcast1, sequencerName, epoch); InternalSequencer sequencer2 = (InternalSequencer) buildSnowcastSequencer(snowcast2, sequencerName, epoch); NodeSequencerService sequencerService1 = (NodeSequencerService) sequencer1.getSequencerService(); NodeSequencerService sequencerService2 = (NodeSequencerService) sequencer2.getSequencerService(); PartitionService partitionService = hazelcastInstance1.getPartitionService(); int partitionId = partitionService.getPartition(sequencerName).getPartitionId(); final SequencerPartition partition1 = sequencerService1.getSequencerPartition(partitionId); final SequencerPartition partition2 = sequencerService2.getSequencerPartition(partitionId); assertTrueEventually(new AssertTask() { @Override public void run() throws Exception { SequencerDefinition sequencerDefinition1 = partition1.getSequencerDefinition(sequencerName); SequencerDefinition sequencerDefinition2 = partition2.getSequencerDefinition(sequencerName); assertEquals(sequencerDefinition1, sequencerDefinition2); } }); } finally { HazelcastClient.shutdownAll(); Hazelcast.shutdownAll(); } }
@Override public PartitionService getPartitionService() { return getHazelcastInstance().getPartitionService(); }
@Test public void testQuery() { List<Book> books = Arrays.asList( new Book("978-4774169316", "Javaエンジニア養成読本", 2138), new Book("978-4798124605", "Beginning Java EE 6 GlassFish 3で始めるエンタープライズJava", 4536), new Book("978-4873117188", "Javaパフォーマンス", 4212) ); withHazelcast(3, hazelcast -> { IMap<String, Book> map = hazelcast.getMap("default"); books.stream().forEach(b -> map.put(b.getIsbn(), b)); PartitionService ps = hazelcast.getPartitionService(); System.out.printf( "%s:%s => %s%n", "978-4774169316", "Javaエンジニア養成読本", ps.getPartition("978-4774169316").getOwner() ); System.out.printf( "%s:%s => %s%n", "978-4798124605", "Beginning Java EE 6 GlassFish 3で始めるエンタープライズJava", ps.getPartition("978-4798124605").getOwner() ); System.out.printf( "%s:%s => %s%n", "978-4873117188", "Javaパフォーマンス", ps.getPartition("978-4873117188").getOwner() ); SqlPredicate titleQuery = new SqlPredicate("title = 'Javaエンジニア養成読本'"); Collection<Book> booksByTitleQuery = map.values(titleQuery); assertThat(booksByTitleQuery) .hasSize(1) .containsOnly(new Book("978-4774169316", "Javaエンジニア養成読本", 2138)); SqlPredicate titleWithLikeQuery = new SqlPredicate("title LIKE '%Java%' AND title LIkE '%養成読本'"); Collection<Book> booksByTitleWithLikeQuery = map.values(titleWithLikeQuery); assertThat(booksByTitleWithLikeQuery) .hasSize(1) .containsOnly(new Book("978-4774169316", "Javaエンジニア養成読本", 2138)); SqlPredicate priceQuery = new SqlPredicate("price > 4000"); Collection<Book> booksByPriceQuery = map.values(priceQuery); assertThat(booksByPriceQuery) .hasSize(2) .containsSequence( new Book("978-4873117188", "Javaパフォーマンス", 4212), new Book("978-4798124605", "Beginning Java EE 6 GlassFish 3で始めるエンタープライズJava", 4536) ); }); }
@Test public void dataAffinity() { Category springCategory = Category.create("spring"); Category javaeeCategory = Category.create("javaee"); Book[] springBooks = { Book.create("978-4798142470", "Spring徹底入門 Spring FrameworkによるJavaアプリケーション開発", 4320, springCategory), Book.create("978-4774182179", "[改訂新版]Spring入門 ――Javaフレームワーク・より良い設計とアーキテクチャ", 4104, springCategory), Book.create("978-4777519699", "はじめてのSpring Boot―スプリング・フレームワークで簡単Javaアプリ開発", 2700, springCategory) }; Book[] javaeeBooks = { Book.create("978-4774183169", "パーフェクト Java EE", 3456, javaeeCategory), Book.create("978-4798140926", "Java EE 7徹底入門 標準Javaフレームワークによる高信頼性Webシステムの構築", 4104, javaeeCategory), Book.create("978-4798124605", "Beginning Java EE 6~GlassFish 3で始めるエンタープライズJava", 4536, javaeeCategory) }; withHazelcastInstance(3, hazelcast -> { IMap<String, Category> categoryMap = hazelcast.getMap("categories"); IMap<BookKey, Book> bookMap = hazelcast.getMap("books"); categoryMap.put(springCategory.getName(), springCategory); categoryMap.put(javaeeCategory.getName(), javaeeCategory); Arrays.stream(springBooks).forEach(book -> bookMap.put(BookKey.create(book.getIsbn(), book.getCategory()), book)); Arrays.stream(javaeeBooks).forEach(book -> bookMap.put(BookKey.create(book.getIsbn(), book.getCategory()), book)); PartitionService ps = hazelcast.getPartitionService(); assertThat( Arrays .stream(springBooks) .map(book -> ps.getPartition(BookKey.create(book.getIsbn(), book.getCategory())).getOwner().getUuid()) .collect(Collectors.toSet()) ) .hasSize(1) .containsOnly(ps.getPartition(springCategory.getName()).getOwner().getUuid()); assertThat( Arrays .stream(springBooks) .map(book -> ps.getPartition(BookKey.create(book.getIsbn(), book.getCategory())).getPartitionId()) .collect(Collectors.toSet()) ) .hasSize(1) .containsOnly(ps.getPartition(springCategory.getName()).getPartitionId()); assertThat( Arrays .stream(javaeeBooks) .map(book -> ps.getPartition(BookKey.create(book.getIsbn(), book.getCategory())).getOwner().getUuid()) .collect(Collectors.toSet()) ) .hasSize(1) .containsOnly(ps.getPartition(javaeeCategory.getName()).getOwner().getUuid()); assertThat( Arrays .stream(javaeeBooks) .map(book -> ps.getPartition(BookKey.create(book.getIsbn(), book.getCategory())).getPartitionId()) .collect(Collectors.toSet()) ) .hasSize(1) .containsOnly(ps.getPartition(javaeeCategory.getName()).getPartitionId()); }); }