private Config hazelcast() { Config conf = new Config(); conf.getNetworkConfig().setPort(hazelCastPort); conf.getNetworkConfig().setPortAutoIncrement(false); conf.setProperty("hazelcast.initial.min.cluster.size", "1"); conf.setProperty("hazelcast.shutdownhook.enabled", "false"); JoinConfig join = conf.getNetworkConfig().getJoin(); boolean isAws = System.getProperty("hazelcast.aws", "false").equals("true"); log.info("Setting up Joiner with this being " + (isAws ? "AWS" : "Multicast")); join.getAwsConfig().setEnabled(isAws); if (isAws) { join.getAwsConfig().setAccessKey(System.getProperty("hazelcast.access-key")); join.getAwsConfig().setSecretKey(System.getProperty("hazelcast.access-secret")); } join.getMulticastConfig().setEnabled(!isAws); ListConfig jobConfig = new ListConfig(); jobConfig.setName(JOBS); conf.addListConfig(jobConfig); ListConfig replicateConfig = new ListConfig(); replicateConfig.setName(REPLICATE_WEIGHTS); conf.addListConfig(replicateConfig); ListConfig topicsConfig = new ListConfig(); topicsConfig.setName(TOPICS); conf.addListConfig(topicsConfig); ListConfig updatesConfig = new ListConfig(); updatesConfig.setName(UPDATES); conf.addListConfig(updatesConfig); ListConfig availableWorkersConfig = new ListConfig(); availableWorkersConfig.setName(AVAILABLE_WORKERS); conf.addListConfig(availableWorkersConfig); MapConfig heartbeatConfig = new MapConfig(); heartbeatConfig.setName(HEART_BEAT); conf.addMapConfig(heartbeatConfig); MapConfig workerEnabledConifg = new MapConfig(); workerEnabledConifg.setName(WORKER_ENABLED); conf.addMapConfig(workerEnabledConifg); return conf; }
@Override public void addWorker(String worker) { heartbeat.put(worker, System.currentTimeMillis()); if (!workers.contains(worker)) { log.info("Adding worker " + worker); workers.add(worker); log.info("Number of workers is now " + workers.size()); } }
@Test public void testTTL() throws Exception { Config config = new Config(); FactoryImpl mockFactory = mock(FactoryImpl.class); // we mocked the node // do not forget to shutdown the connectionManager // so that server socket can be released. Node node = new Node(mockFactory, config); node.serviceThread = Thread.currentThread(); CMap cmap = new CMap(node.concurrentMapManager, "c:myMap"); Object key = "1"; Object value = "istanbul"; Data dKey = toData(key); Data dValue = toData(value); Request reqPut = newPutRequest(dKey, dValue); reqPut.ttl = 3000; cmap.put(reqPut); assertTrue(cmap.mapRecords.containsKey(toData(key))); Data actualValue = cmap.get(newGetRequest(dKey)); assertThat(toObject(actualValue), equalTo(value)); assertEquals(1, cmap.mapRecords.size()); Record record = cmap.getRecord(dKey); assertNotNull(record); assertTrue(record.isActive()); assertTrue(record.isValid()); assertEquals(1, cmap.size()); assertNotNull(cmap.get(newGetRequest(dKey))); assertEquals(dValue, cmap.get(newGetRequest(dKey))); assertTrue(record.getRemainingTTL() > 1000); Thread.sleep(1000); assertTrue(record.getRemainingTTL() < 2100); cmap.put(newPutRequest(dKey, dValue)); assertTrue(record.getRemainingTTL() > 2001); assertTrue(record.isActive()); assertTrue(record.isValid()); Thread.sleep(1000); assertTrue(record.getRemainingTTL() < 2100); cmap.put(newPutRequest(dKey, dValue)); assertTrue(record.getRemainingTTL() > 2001); assertTrue(record.isActive()); assertTrue(record.isValid()); Thread.sleep(5000); assertEquals(0, cmap.size()); assertTrue(cmap.evict(newEvictRequest(dKey))); assertTrue(cmap.shouldPurgeRecord(record, System.currentTimeMillis() + 10000)); cmap.removeAndPurgeRecord(record); assertEquals(0, cmap.mapRecords.size()); assertEquals(0, cmap.size()); assertEquals(0, cmap.mapIndexService.size()); node.connectionManager.shutdown(); }
@Test public void testMapPutTtlWithListener() throws InterruptedException { Config cfg = new Config(); TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2); final HazelcastInstance[] instances = factory.newInstances(cfg); warmUpPartitions(instances); final int k = 10; final int putCount = 10000; final CountDownLatch latch = new CountDownLatch(k * putCount); final IMap map = instances[0].getMap("testMapEvictionTtlWithListener"); final AtomicBoolean error = new AtomicBoolean(false); final Set<Long> times = Collections.newSetFromMap(new ConcurrentHashMap<Long, Boolean>()); map.addEntryListener( new EntryAdapter() { public void entryEvicted(final EntryEvent event) { final Long expectedEvictionTime = (Long) (event.getOldValue()); long timeDifference = System.currentTimeMillis() - expectedEvictionTime; if (timeDifference > 5000) { error.set(true); times.add(timeDifference); } latch.countDown(); } }, true); for (int i = 0; i < k; i++) { final int threadId = i; int ttl = (int) (Math.random() * 5000 + 3000); for (int j = 0; j < putCount; j++) { final long expectedEvictionTime = ttl + System.currentTimeMillis(); map.put(j + putCount * threadId, expectedEvictionTime, ttl, TimeUnit.MILLISECONDS); } } assertTrue(latch.await(1, TimeUnit.MINUTES)); assertFalse("Some evictions took more than 3 seconds! -> " + times, error.get()); }
/* github issue 304 */ @Test public void testIssue304EvictionDespitePut() throws InterruptedException { Config c = new Config(); c.getGroupConfig().setName("testIssue304EvictionDespitePut"); final HashMap<String, MapConfig> mapConfigs = new HashMap<String, MapConfig>(); final MapConfig value = new MapConfig(); value.setMaxIdleSeconds(3); mapConfigs.put("default", value); c.setMapConfigs(mapConfigs); final Properties properties = new Properties(); properties.setProperty("hazelcast.map.cleanup.delay.seconds", "1"); // we need faster cleanups c.setProperties(properties); int n = 1; TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(n); final HazelcastInstance hazelcastInstance = factory.newHazelcastInstance(c); IMap<String, Long> map = hazelcastInstance.getMap("testIssue304EvictionDespitePutMap"); final AtomicInteger evictCount = new AtomicInteger(0); map.addEntryListener( new EntryListener<String, Long>() { public void entryAdded(EntryEvent<String, Long> event) {} public void entryRemoved(EntryEvent<String, Long> event) {} public void entryUpdated(EntryEvent<String, Long> event) {} public void entryEvicted(EntryEvent<String, Long> event) { evictCount.incrementAndGet(); } }, true); String key = "key"; for (int i = 0; i < 5; i++) { map.put(key, System.currentTimeMillis()); Thread.sleep(1000); } assertEquals(evictCount.get(), 0); assertNotNull(map.get(key)); hazelcastInstance.getLifecycleService().shutdown(); }
@Test public void testPut() throws Exception { Config config = new Config(); FactoryImpl mockFactory = mock(FactoryImpl.class); // we mocked the node // do not forget to shutdown the connectionManager // so that server socket can be released. Node node = new Node(mockFactory, config); node.serviceThread = Thread.currentThread(); CMap cmap = new CMap(node.concurrentMapManager, "c:myMap"); Object key = "1"; Object value = "istanbul"; Data dKey = toData(key); Data dValue = toData(value); cmap.put(newPutRequest(dKey, dValue)); assertTrue(cmap.mapRecords.containsKey(toData(key))); Data actualValue = cmap.get(newGetRequest(dKey)); assertThat(toObject(actualValue), equalTo(value)); assertEquals(1, cmap.mapRecords.size()); Record record = cmap.getRecord(dKey); assertNotNull(record); assertTrue(record.isActive()); assertTrue(record.isValid()); assertEquals(1, cmap.size()); cmap.remove(newRemoveRequest(dKey)); assertTrue(System.currentTimeMillis() - record.getRemoveTime() < 100); assertEquals(1, cmap.mapRecords.size()); record = cmap.getRecord(dKey); assertNotNull(record); assertFalse(record.isActive()); assertFalse(record.isValid()); assertEquals(0, cmap.size()); cmap.put(newPutRequest(dKey, dValue, 1000)); assertEquals(0, record.getRemoveTime()); assertTrue(cmap.mapRecords.containsKey(toData(key))); Thread.sleep(1500); assertEquals(0, cmap.size()); assertFalse(cmap.containsKey(newContainsRequest(dKey, null))); node.connectionManager.shutdown(); }
/** * Attempts to receive a message using the given strategy. The method will continue to attempt to * receive until either {@link ReceiveStrategy#isRetryable()} returns false, a message is * received, or the consumer is stopped. * * @param strategy the strategy to use for receiving the message and determining retries * @return the message or null if no message was received */ private HazelcastMQMessage doReceive(ReceiveStrategy strategy) { HazelcastMQMessage msg = null; do { receiveLock.lock(); try { IQueue<byte[]> queue = hazelcastMQContext.resolveQueue(destination); if (queue == null && topicListener == null) { throw new HazelcastMQException( format("Destination cannot be resolved [%s].", destination)); } else if (queue == null) { queue = topicListener.getQueue(); } byte[] msgData = strategy.receive(queue); if (msgData != null) { msg = config.getMessageConverter().toMessage(msgData); } // Check for message expiration if we have a message with expiration // time. if (msg != null && msg.getHeaders().get(Headers.EXPIRATION) != null) { long expirationTime = Long.parseLong(msg.getHeaders().get(Headers.EXPIRATION)); if (expirationTime != 0 && expirationTime <= System.currentTimeMillis()) { log.info("Dropping message [{}] because it has expired.", msg.getId()); msg = null; } } } finally { receiveLock.unlock(); } } while (msg == null && !closed && strategy.isRetryable()); return msg; }
@Test public void testSubmitToMembersRunnable() throws InterruptedException { final int k = simpleTestNodeCount; TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k); final HazelcastInstance[] instances = factory.newInstances(new Config()); final AtomicInteger count = new AtomicInteger(0); final CountDownLatch latch = new CountDownLatch(k); final MultiExecutionCallback callback = new MultiExecutionCallback() { public void onResponse(Member member, Object value) { count.incrementAndGet(); } public void onComplete(Map<Member, Object> values) { latch.countDown(); } }; int sum = 0; final Set<Member> membersSet = instances[0].getCluster().getMembers(); final Member[] members = membersSet.toArray(new Member[membersSet.size()]); final Random random = new Random(); for (int i = 0; i < k; i++) { final IExecutorService service = instances[i].getExecutorService("testSubmitToMembersRunnable"); final String script = "hazelcast.getAtomicLong('testSubmitToMembersRunnable').incrementAndGet();"; final int n = random.nextInt(k) + 1; sum += n; Member[] m = new Member[n]; System.arraycopy(members, 0, m, 0, n); service.submitToMembers(new ScriptRunnable(script, null), Arrays.asList(m), callback); } assertTrue(latch.await(30, TimeUnit.SECONDS)); final IAtomicLong result = instances[0].getAtomicLong("testSubmitToMembersRunnable"); assertEquals(sum, result.get()); assertEquals(sum, count.get()); }
public static void main(String[] args) { System.setProperty("hazelcast.mc.topic.excludes", "pm.*"); new Node().init(); }
// void processEvents() { public void run() { System.out.println("WatchDir Thread INFO: priority=" + Thread.currentThread().getPriority()); for (; ; ) { // wait for key to be signalled System.out.println("WatchDir INFO: restarting loop...acquiring new key"); WatchKey key; try { key = watcher.take(); } catch (InterruptedException x) { return; } Path dir = keys.get(key); if (dir == null) { System.err.println("WatchKey not recognized!!"); continue; } for (WatchEvent<?> event : key.pollEvents()) { WatchEvent.Kind kind = event.kind(); // TBD - provide example of how OVERFLOW event is handled if (kind == OVERFLOW) { System.out.println("Encountered OVERFLOW Event - " + event); continue; } // Context for directory entry event is the file name of entry WatchEvent<Path> ev = cast(event); Path name = ev.context(); Path child = dir.resolve(name); // print out event System.out.format("[WatchDir] %s: %s\n", event.kind().name(), child); // if directory is created, and watching recursively, then // register it and its sub-directories if (recursive && (kind == ENTRY_CREATE)) { try { if (Files.isDirectory(child, NOFOLLOW_LINKS)) { registerAll(child); } } catch (IOException x) { // ignore to keep sample readbale } } long t = System.currentTimeMillis(); if (!Folder.dontWatch.contains(Folder.getInternalPath(child))) { Thread.currentThread().setPriority(Thread.NORM_PRIORITY); System.out.println( "WatchDir#" + key + " INFO: path=" + child + ", internal=" + Folder.getInternalPath(child) + " is NOT in don't watch list. Forwarding it to other peers. @" + Main.timeToString(t)); // DEBUG forwardToItopic(kind, child); } else { Thread.currentThread().setPriority(Thread.MIN_PRIORITY); System.out.println( "WatchDir#" + key + " INFO: path=" + child + ", internal=" + Folder.getInternalPath(child) + " IS in the don't watch list. NOT forwarding. @" + Main.timeToString(t)); // DEBUG // try{ // Thread.sleep(minDelayBtwnWatchEvents); // } catch(InterruptedException ex) { // System.err.println("Exception:"+ex+" while trying to sleep WatchDir thread"); // ex.printStackTrace(); // } } } // reset key and remove from set if directory no longer accessible boolean valid = key.reset(); if (!valid) { keys.remove(key); // all directories are inaccessible if (keys.isEmpty()) { break; } } } }
protected void handleCommand(String command) { if (command.contains("__")) { namespace = command.split("__")[0]; command = command.substring(command.indexOf("__") + 2); } if (echo) { if (Thread.currentThread().getName().toLowerCase().indexOf("main") < 0) println(" [" + Thread.currentThread().getName() + "] " + command); else println(command); } if (command == null || command.startsWith("//")) return; command = command.trim(); if (command == null || command.length() == 0) { return; } String first = command; int spaceIndex = command.indexOf(' '); String[] argsSplit = command.split(" "); String[] args = new String[argsSplit.length]; for (int i = 0; i < argsSplit.length; i++) { args[i] = argsSplit[i].trim(); } if (spaceIndex != -1) { first = args[0]; } if (command.startsWith("help")) { handleHelp(command); } else if (first.startsWith("#") && first.length() > 1) { int repeat = Integer.parseInt(first.substring(1)); long t0 = Clock.currentTimeMillis(); for (int i = 0; i < repeat; i++) { handleCommand(command.substring(first.length()).replaceAll("\\$i", "" + i)); } println("ops/s = " + repeat * 1000 / (Clock.currentTimeMillis() - t0)); return; } else if (first.startsWith("&") && first.length() > 1) { final int fork = Integer.parseInt(first.substring(1)); ExecutorService pool = Executors.newFixedThreadPool(fork); final String threadCommand = command.substring(first.length()); for (int i = 0; i < fork; i++) { final int threadID = i; pool.submit( new Runnable() { public void run() { String command = threadCommand; String[] threadArgs = command.replaceAll("\\$t", "" + threadID).trim().split(" "); // TODO &t #4 m.putmany x k if ("m.putmany".equals(threadArgs[0]) || "m.removemany".equals(threadArgs[0])) { if (threadArgs.length < 4) { command += " " + Integer.parseInt(threadArgs[1]) * threadID; } } handleCommand(command); } }); } pool.shutdown(); try { pool.awaitTermination(60 * 60, TimeUnit.SECONDS); // wait 1h } catch (Exception e) { e.printStackTrace(); } } else if (first.startsWith("@")) { if (first.length() == 1) { println("usage: @<file-name>"); return; } File f = new File(first.substring(1)); println("Executing script file " + f.getAbsolutePath()); if (f.exists()) { try { BufferedReader br = new BufferedReader(new FileReader(f)); String l = br.readLine(); while (l != null) { handleCommand(l); l = br.readLine(); } br.close(); } catch (IOException e) { e.printStackTrace(); } } else { println("File not found! " + f.getAbsolutePath()); } } else if (command.indexOf(';') != -1) { StringTokenizer st = new StringTokenizer(command, ";"); while (st.hasMoreTokens()) { handleCommand(st.nextToken()); } return; } else if ("silent".equals(first)) { silent = Boolean.parseBoolean(args[1]); } else if ("shutdown".equals(first)) { hazelcast.getLifecycleService().shutdown(); } else if ("echo".equals(first)) { echo = Boolean.parseBoolean(args[1]); println("echo: " + echo); } else if ("ns".equals(first)) { if (args.length > 1) { namespace = args[1]; println("namespace: " + namespace); // init(); } } else if ("whoami".equals(first)) { println(hazelcast.getCluster().getLocalMember()); } else if ("who".equals(first)) { println(hazelcast.getCluster()); } else if ("jvm".equals(first)) { System.gc(); println("Memory max: " + Runtime.getRuntime().maxMemory() / 1024 / 1024 + "M"); println( "Memory free: " + Runtime.getRuntime().freeMemory() / 1024 / 1024 + "M " + (int) (Runtime.getRuntime().freeMemory() * 100 / Runtime.getRuntime().maxMemory()) + "%"); long total = Runtime.getRuntime().totalMemory(); long free = Runtime.getRuntime().freeMemory(); println("Used Memory:" + ((total - free) / 1024 / 1024) + "MB"); println("# procs: " + Runtime.getRuntime().availableProcessors()); println( "OS info: " + ManagementFactory.getOperatingSystemMXBean().getArch() + " " + ManagementFactory.getOperatingSystemMXBean().getName() + " " + ManagementFactory.getOperatingSystemMXBean().getVersion()); println( "JVM: " + ManagementFactory.getRuntimeMXBean().getVmVendor() + " " + ManagementFactory.getRuntimeMXBean().getVmName() + " " + ManagementFactory.getRuntimeMXBean().getVmVersion()); } else if (first.indexOf("ock") != -1 && first.indexOf(".") == -1) { handleLock(args); } else if (first.indexOf(".size") != -1) { handleSize(args); } else if (first.indexOf(".clear") != -1) { handleClear(args); } else if (first.indexOf(".destroy") != -1) { handleDestroy(args); } else if (first.indexOf(".iterator") != -1) { handleIterator(args); } else if (first.indexOf(".contains") != -1) { handleContains(args); } else if (first.indexOf(".stats") != -1) { handStats(args); } else if ("t.publish".equals(first)) { handleTopicPublish(args); } else if ("q.offer".equals(first)) { handleQOffer(args); } else if ("q.take".equals(first)) { handleQTake(args); } else if ("q.poll".equals(first)) { handleQPoll(args); } else if ("q.peek".equals(first)) { handleQPeek(args); } else if ("q.capacity".equals(first)) { handleQCapacity(args); } else if ("q.offermany".equals(first)) { handleQOfferMany(args); } else if ("q.pollmany".equals(first)) { handleQPollMany(args); } else if ("s.add".equals(first)) { handleSetAdd(args); } else if ("s.remove".equals(first)) { handleSetRemove(args); } else if ("s.addmany".equals(first)) { handleSetAddMany(args); } else if ("s.removemany".equals(first)) { handleSetRemoveMany(args); } else if (first.equals("m.replace")) { handleMapReplace(args); } else if (first.equalsIgnoreCase("m.putIfAbsent")) { handleMapPutIfAbsent(args); } else if (first.equals("m.putAsync")) { handleMapPutAsync(args); } else if (first.equals("m.getAsync")) { handleMapGetAsync(args); } else if (first.equals("m.put")) { handleMapPut(args); } else if (first.equals("m.get")) { handleMapGet(args); } else if (first.equalsIgnoreCase("m.getMapEntry")) { handleMapGetMapEntry(args); } else if (first.equals("m.remove")) { handleMapRemove(args); } else if (first.equals("m.evict")) { handleMapEvict(args); } else if (first.equals("m.putmany") || first.equalsIgnoreCase("m.putAll")) { handleMapPutMany(args); } else if (first.equals("m.getmany")) { handleMapGetMany(args); } else if (first.equals("m.removemany")) { handleMapRemoveMany(args); } else if (command.equalsIgnoreCase("m.localKeys")) { handleMapLocalKeys(); } else if (command.equals("m.keys")) { handleMapKeys(); } else if (command.equals("m.values")) { handleMapValues(); } else if (command.equals("m.entries")) { handleMapEntries(); } else if (first.equals("m.lock")) { handleMapLock(args); } else if (first.equalsIgnoreCase("m.tryLock")) { handleMapTryLock(args); } else if (first.equals("m.unlock")) { handleMapUnlock(args); } else if (first.indexOf(".addListener") != -1) { handleAddListener(args); } else if (first.equals("m.removeMapListener")) { handleRemoveListener(args); } else if (first.equals("m.unlock")) { handleMapUnlock(args); } else if (first.equals("l.add")) { handleListAdd(args); } else if (first.equals("l.set")) { handleListSet(args); } else if ("l.addmany".equals(first)) { handleListAddMany(args); } else if (first.equals("l.remove")) { handleListRemove(args); } else if (first.equals("l.contains")) { handleListContains(args); } else if ("a.get".equals(first)) { handleAtomicNumberGet(args); } else if ("a.set".equals(first)) { handleAtomicNumberSet(args); } else if ("a.inc".equals(first)) { handleAtomicNumberInc(args); } else if ("a.dec".equals(first)) { handleAtomicNumberDec(args); // } else if (first.equals("execute")) { // execute(args); } else if (first.equals("partitions")) { handlePartitions(args); // } else if (first.equals("txn")) { // hazelcast.getTransaction().begin(); // } else if (first.equals("commit")) { // hazelcast.getTransaction().commit(); // } else if (first.equals("rollback")) { // hazelcast.getTransaction().rollback(); // } else if (first.equalsIgnoreCase("executeOnKey")) { // executeOnKey(args); // } else if (first.equalsIgnoreCase("executeOnMember")) { // executeOnMember(args); // } else if (first.equalsIgnoreCase("executeOnMembers")) { // executeOnMembers(args); // } else if (first.equalsIgnoreCase("longOther") || // first.equalsIgnoreCase("executeLongOther")) { // executeLongTaskOnOtherMember(args); // } else if (first.equalsIgnoreCase("long") || first.equalsIgnoreCase("executeLong")) // { // executeLong(args); } else if (first.equalsIgnoreCase("instances")) { handleInstances(args); } else if (first.equalsIgnoreCase("quit") || first.equalsIgnoreCase("exit")) { System.exit(0); } else { println("type 'help' for help"); } }
public Node(HazelcastInstanceImpl hazelcastInstance, Config config, NodeContext nodeContext) { this.hazelcastInstance = hazelcastInstance; this.threadGroup = hazelcastInstance.threadGroup; this.config = config; configClassLoader = config.getClassLoader(); this.groupProperties = new GroupProperties(config); SerializationService ss; try { ss = new SerializationServiceBuilder() .setClassLoader(configClassLoader) .setConfig(config.getSerializationConfig()) .setManagedContext(hazelcastInstance.managedContext) .setHazelcastInstance(hazelcastInstance) .build(); } catch (Exception e) { throw ExceptionUtil.rethrow(e); } serializationService = (SerializationServiceImpl) ss; systemLogService = new SystemLogService(groupProperties.SYSTEM_LOG_ENABLED.getBoolean()); final AddressPicker addressPicker = nodeContext.createAddressPicker(this); try { addressPicker.pickAddress(); } catch (Throwable e) { throw ExceptionUtil.rethrow(e); } final ServerSocketChannel serverSocketChannel = addressPicker.getServerSocketChannel(); address = addressPicker.getPublicAddress(); localMember = new MemberImpl(address, true, UuidUtil.createMemberUuid(address)); String loggingType = groupProperties.LOGGING_TYPE.getString(); loggingService = new LoggingServiceImpl( systemLogService, config.getGroupConfig().getName(), loggingType, localMember); logger = loggingService.getLogger(Node.class.getName()); initializer = NodeInitializerFactory.create(configClassLoader); try { initializer.beforeInitialize(this); } catch (Throwable e) { try { serverSocketChannel.close(); } catch (Throwable ignored) { } throw ExceptionUtil.rethrow(e); } securityContext = config.getSecurityConfig().isEnabled() ? initializer.getSecurityContext() : null; nodeEngine = new NodeEngineImpl(this); clientEngine = new ClientEngineImpl(this); connectionManager = nodeContext.createConnectionManager(this, serverSocketChannel); partitionService = new PartitionServiceImpl(this); clusterService = new ClusterServiceImpl(this); textCommandService = new TextCommandServiceImpl(this); initializer.printNodeInfo(this); buildNumber = initializer.getBuildNumber(); VersionCheck.check(this, initializer.getBuild(), initializer.getVersion()); JoinConfig join = config.getNetworkConfig().getJoin(); MulticastService mcService = null; try { if (join.getMulticastConfig().isEnabled()) { MulticastConfig multicastConfig = join.getMulticastConfig(); MulticastSocket multicastSocket = new MulticastSocket(null); multicastSocket.setReuseAddress(true); // bind to receive interface multicastSocket.bind(new InetSocketAddress(multicastConfig.getMulticastPort())); multicastSocket.setTimeToLive(multicastConfig.getMulticastTimeToLive()); try { // set the send interface final Address bindAddress = addressPicker.getBindAddress(); // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4417033 // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6402758 if (!bindAddress.getInetAddress().isLoopbackAddress()) { multicastSocket.setInterface(bindAddress.getInetAddress()); } } catch (Exception e) { logger.warning(e); } multicastSocket.setReceiveBufferSize(64 * 1024); multicastSocket.setSendBufferSize(64 * 1024); String multicastGroup = System.getProperty("hazelcast.multicast.group"); if (multicastGroup == null) { multicastGroup = multicastConfig.getMulticastGroup(); } multicastConfig.setMulticastGroup(multicastGroup); multicastSocket.joinGroup(InetAddress.getByName(multicastGroup)); multicastSocket.setSoTimeout(1000); mcService = new MulticastService(this, multicastSocket); mcService.addMulticastListener(new NodeMulticastListener(this)); } } catch (Exception e) { logger.severe(e); } this.multicastService = mcService; initializeListeners(config); joiner = nodeContext.createJoiner(this); }
@BeforeClass public static void init() throws Exception { System.setProperty(GroupProperties.PROP_WAIT_SECONDS_BEFORE_JOIN, "1"); System.setProperty(GroupProperties.PROP_VERSION_CHECK_ENABLED, "false"); Hazelcast.shutdownAll(); }
@Test public void testPutWithTwoMember() throws Exception { Config config = new Config(); HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config); HazelcastInstance h2 = Hazelcast.newHazelcastInstance(config); assertEquals(2, h1.getCluster().getMembers().size()); assertEquals(2, h2.getCluster().getMembers().size()); IMap imap1 = h1.getMap("default"); IMap imap2 = h2.getMap("default"); assertEquals(0, imap1.size()); assertEquals(0, imap2.size()); CMap cmap1 = getCMap(h1, "default"); CMap cmap2 = getCMap(h2, "default"); assertNotNull(cmap1); assertNotNull(cmap2); Object key = "1"; Object value = "value"; Data dKey = toData(key); Data dValue = toData(value); imap1.put(key, value, 5, TimeUnit.SECONDS); assertEquals(1, cmap1.mapRecords.size()); assertEquals(1, cmap2.mapRecords.size()); assertEquals( 1, cmap1.getMapIndexService().getOwnedRecords().size() + cmap2.getMapIndexService().getOwnedRecords().size()); Record record1 = cmap1.getRecord(dKey); Record record2 = cmap2.getRecord(dKey); long now = System.currentTimeMillis(); long millisLeft1 = record1.getExpirationTime() - now; long millisLeft2 = record2.getExpirationTime() - now; assertTrue(millisLeft1 <= 5000 && millisLeft1 > 0); assertTrue(millisLeft2 <= 5000 && millisLeft2 > 0); assertTrue(record1.isActive()); assertTrue(record2.isActive()); assertEquals(1, record1.valueCount()); assertEquals(1, record2.valueCount()); assertEquals(dValue, record1.getValueData()); assertEquals(dValue, record2.getValueData()); imap1.set("2", "value2", 5, TimeUnit.SECONDS); assertEquals("value2", imap1.get("2")); assertEquals("value2", imap2.get("2")); Thread.sleep(6000); assertNull(imap1.get("2")); assertNull(imap2.get("2")); now = System.currentTimeMillis(); assertFalse(record1.isValid(now)); assertFalse(record2.isValid(now)); Thread.sleep(23000); assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size()); assertEquals(0, cmap2.getMapIndexService().getOwnedRecords().size()); assertEquals(0, cmap1.mapRecords.size()); assertEquals(0, cmap2.mapRecords.size()); imap1.put(key, value, 10, TimeUnit.SECONDS); assertTrue(migrateKey(key, h1, h1, 0)); assertTrue(migrateKey(key, h1, h2, 1)); assertEquals(1, cmap1.mapRecords.size()); assertEquals(1, cmap2.mapRecords.size()); assertEquals( 1, cmap1.getMapIndexService().getOwnedRecords().size() + cmap2.getMapIndexService().getOwnedRecords().size()); record1 = cmap1.getRecord(dKey); record2 = cmap2.getRecord(dKey); now = System.currentTimeMillis(); millisLeft1 = record1.getExpirationTime() - now; millisLeft2 = record2.getExpirationTime() - now; assertTrue(millisLeft1 <= 11000 && millisLeft1 > 0); assertTrue(millisLeft2 <= 11000 && millisLeft2 > 0); assertTrue(record1.isActive()); assertTrue(record2.isActive()); assertTrue(record1.isValid(now)); assertTrue(record2.isValid(now)); assertEquals(1, record1.valueCount()); assertEquals(1, record2.valueCount()); assertEquals(1, cmap1.mapRecords.size()); assertEquals(1, cmap2.mapRecords.size()); assertEquals( 1, cmap1.getMapIndexService().getOwnedRecords().size() + cmap2.getMapIndexService().getOwnedRecords().size()); assertTrue(migrateKey(key, h1, h2, 0)); assertTrue(migrateKey(key, h1, h1, 1)); cmap1.startCleanup(true); cmap2.startCleanup(true); assertEquals(1, cmap1.mapRecords.size()); assertEquals(1, cmap2.mapRecords.size()); assertEquals(1, cmap2.getMapIndexService().getOwnedRecords().size()); assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size()); now = System.currentTimeMillis(); millisLeft1 = record1.getExpirationTime() - now; millisLeft2 = record2.getExpirationTime() - now; assertTrue(millisLeft1 <= 10000 && millisLeft1 > 0); assertTrue(millisLeft2 <= 10000 && millisLeft2 > 0); assertTrue(record1.isActive()); assertTrue(record2.isActive()); assertTrue(record1.isValid(now)); assertTrue(record2.isValid(now)); assertEquals(1, record1.valueCount()); assertEquals(1, record2.valueCount()); Thread.sleep(11000); now = System.currentTimeMillis(); assertFalse(record1.isValid(now)); assertFalse(record2.isValid(now)); Thread.sleep(20000); assertEquals(0, cmap1.mapRecords.size()); assertEquals(0, cmap2.mapRecords.size()); assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size()); assertEquals(0, cmap2.getMapIndexService().getOwnedRecords().size()); imap1.put("1", "value1"); record1 = cmap1.getRecord(dKey); record2 = cmap2.getRecord(dKey); assertEquals(1, cmap1.mapRecords.size()); assertEquals(1, cmap2.mapRecords.size()); assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size()); assertEquals(1, cmap2.getMapIndexService().getOwnedRecords().size()); now = System.currentTimeMillis(); assertEquals(Long.MAX_VALUE, record1.getExpirationTime()); assertEquals(Long.MAX_VALUE, record2.getExpirationTime()); assertTrue(record1.isActive()); assertTrue(record2.isActive()); assertTrue(record1.isValid(now)); assertTrue(record2.isValid(now)); assertEquals(1, record1.valueCount()); assertEquals(1, record2.valueCount()); imap1.remove("1"); assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size()); assertEquals(0, cmap2.getMapIndexService().getOwnedRecords().size()); Thread.sleep(20000); assertEquals(0, cmap1.mapRecords.size()); assertEquals(0, cmap2.mapRecords.size()); assertEquals(0, cmap1.mapIndexService.size()); assertEquals(0, cmap2.mapIndexService.size()); }
public long findDelayMillis(ScheduledEntry entry) { long diffMillis = (System.nanoTime() - entry.getScheduleTimeNanos()) / 1000000; return Math.max(0, entry.getScheduledDelayMillis() - diffMillis); }