@BeforeClass
 public static void init() {
   instance1 = Hazelcast.newHazelcastInstance();
   instance2 = Hazelcast.newHazelcastInstance();
   instance3 = Hazelcast.newHazelcastInstance();
   client = HazelcastClient.newHazelcastClient();
 }
Example #2
0
 @Test
 @Ignore
 public void testCallState() throws Exception {
   Config config = new Config();
   final HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config);
   final HazelcastInstance h2 = Hazelcast.newHazelcastInstance(config);
   final Node node1 = getNode(h1);
   final Node node2 = getNode(h2);
   Thread.sleep(100);
   final CountDownLatch latch = new CountDownLatch(1);
   final IMap imap1 = h1.getMap("default");
   new Thread(
           new Runnable() {
             public void run() {
               imap1.lock("1");
               latch.countDown();
             }
           })
       .start();
   latch.await();
   //        final IMap imap2 = h2.getMap("default");
   final AtomicInteger threadId = new AtomicInteger();
   new Thread(
           new Runnable() {
             public void run() {
               ThreadContext.get().setCurrentFactory(node1.factory);
               threadId.set(ThreadContext.get().getThreadId());
               imap1.put("1", "value1");
             }
           })
       .start();
   Thread.sleep(1000);
   System.out.println(node1.getThisAddress() + " thread " + threadId.get());
   CallState callState1 =
       node1.getSystemLogService().getCallState(node1.getThisAddress(), threadId.get());
   if (callState1 != null) {
     for (Object callStateLog : callState1.getLogs()) {
       System.out.println(callStateLog);
     }
   }
   CallState callState2 =
       node2.getSystemLogService().getCallState(node1.getThisAddress(), threadId.get());
   System.out.println("========================");
   if (callState2 != null) {
     for (Object callStateLog : callState2.getLogs()) {
       System.out.println(callStateLog);
     }
   }
 }
    public static void main(String[] args) throws Exception {
        HazelcastInstance hz = Hazelcast.newHazelcastInstance();
        Cluster cluster = hz.getCluster();

        cluster.addMembershipListener(new MembershipListener() {
            @Override
            public void memberAdded(MembershipEvent membershipEvent) {
                System.out.println( "********** MemberAdded " + membershipEvent );
            }

            @Override
            public void memberRemoved(MembershipEvent membershipEvent) {
                System.out.println( "********** MemberRemoved " + membershipEvent );
            }

            @Override
            public void memberAttributeChanged(MemberAttributeEvent memberAttributeEvent) {
                System.out.println( "********** MemberAttributeChanged " + memberAttributeEvent );
            }
        });
Member localMember = cluster.getLocalMember();
        System.out.println ( "********** my inetAddress= " + localMember.getInetSocketAddress() );


    }
  @Test
  public void testConfigLifecycleListener() throws InterruptedException {
    ClientConfig config = new ClientConfig();
    final BlockingQueue<LifecycleEvent> q = new LinkedBlockingQueue<LifecycleEvent>();
    config.addListener(
        new LifecycleListener() {
          public void stateChanged(final LifecycleEvent event) {
            q.offer(event);
            System.out.println(event);
          }
        });
    Hazelcast.getDefaultInstance();
    HazelcastClient client = HazelcastClient.newHazelcastClient(config);

    Assert.assertEquals(new LifecycleEvent(LifecycleState.STARTING), q.poll(3, TimeUnit.SECONDS));
    Assert.assertEquals(
        new LifecycleEvent(LifecycleState.CLIENT_CONNECTION_OPENING), q.poll(3, TimeUnit.SECONDS));
    Assert.assertEquals(
        new LifecycleEvent(LifecycleState.CLIENT_CONNECTION_OPENED), q.poll(3, TimeUnit.SECONDS));
    Assert.assertEquals(new LifecycleEvent(LifecycleState.STARTED), q.poll(3, TimeUnit.SECONDS));
    client.shutdown();
    //        Assert.assertEquals(new LifecycleEvent(LifecycleState.CLIENT_CONNECTION_LOST),
    // q.poll(3, TimeUnit.SECONDS));
    Assert.assertEquals(
        new LifecycleEvent(LifecycleState.SHUTTING_DOWN), q.poll(3, TimeUnit.SECONDS));
    Assert.assertEquals(new LifecycleEvent(LifecycleState.SHUTDOWN), q.poll(3, TimeUnit.SECONDS));
  }
 @Test
 public void testDisablingSystemLogs() throws Exception {
   Config config = new Config();
   config.setProperty(GroupProperties.PROP_SYSTEM_LOG_ENABLED, "true");
   config.getGroupConfig().setName("testDisablingSystemLogs");
   HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
   HazelcastInstance instance2 = Hazelcast.newHazelcastInstance(config);
   instance.getMap("map").put("key", "value");
   Node node = TestUtil.getNode(instance);
   assertTrue(node.getSystemLogService().getLogBundle().size() > 0);
   Hazelcast.shutdownAll();
   config.setProperty(GroupProperties.PROP_SYSTEM_LOG_ENABLED, "false");
   instance = Hazelcast.newHazelcastInstance(config);
   instance2 = Hazelcast.newHazelcastInstance(config);
   instance.getMap("map").put("key2", "value2");
   assertTrue(node.getSystemLogService().getLogBundle().size() == 0);
 }
  /* github issue #183 */
  @Test
  public void testKeyBasedListeners() throws InterruptedException {
    try {
      Config config = new Config();
      HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
      IMap<String, String> map = instance.getMap("map");
      map.put("key1", "value1");
      map.put("key2", "value2");
      map.put("key3", "value3");

      ClientConfig clientConfig = new ClientConfig();
      HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);

      final AtomicInteger count = new AtomicInteger(0);
      IMap<String, String> clientMap = client.getMap("map");

      clientMap.addEntryListener(
          new EntryListener<String, String>() {
            public void entryAdded(EntryEvent<String, String> entryEvent) {
              count.incrementAndGet();
            }

            public void entryRemoved(EntryEvent<String, String> entryEvent) {}

            public void entryUpdated(EntryEvent<String, String> entryEvent) {
              count.incrementAndGet();
            }

            public void entryEvicted(EntryEvent<String, String> entryEvent) {}
          },
          "key1",
          true);

      clientMap.addEntryListener(
          new EntryListener<String, String>() {
            public void entryAdded(EntryEvent<String, String> entryEvent) {
              count.incrementAndGet();
            }

            public void entryRemoved(EntryEvent<String, String> entryEvent) {}

            public void entryUpdated(EntryEvent<String, String> entryEvent) {
              System.out.println("event map");
              count.incrementAndGet();
            }

            public void entryEvicted(EntryEvent<String, String> entryEvent) {}
          },
          "key2",
          true);

      map.put("key1", "new-value1");
      Thread.sleep(100);
      Assert.assertEquals(count.get(), 1);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
 @Start
 public void startHazel() {
   Config configApp = new Config();
   configApp.getGroupConfig().setName("KGroup_HazelGroup_" + getName());
   configApp.setInstanceName("KGroup_HazelGroup_" + getNodeName());
   hazelInstance = Hazelcast.newHazelcastInstance(configApp);
   Cluster cluster = hazelInstance.getCluster();
   cluster.addMembershipListener(this);
 }
Example #8
0
  @BeforeClass
  public static void init() {
    Config config = new Config();

    QueueConfig queueConfig = config.getQueueConfig(queueForTestQueueWithSizeLimit);
    queueConfig.setMaxSize(maxSizeForQueue);

    queueConfig = config.getQueueConfig(queueForTestOfferPoll);
    queueConfig.setMaxSize(maxSizeForQueue);

    server = Hazelcast.newHazelcastInstance(config);
    client = HazelcastClient.newHazelcastClient();
  }
 @Test
 public void testDestroyJustAfterCreate() {
   final HazelcastInstance instance = Hazelcast.newHazelcastInstance();
   instance.addDistributedObjectListener(new EventCountListener());
   IMap<Object, Object> map = instance.getMap(randomString());
   map.destroy();
   AssertTask task =
       new AssertTask() {
         @Override
         public void run() throws Exception {
           Assert.assertEquals(1, EventCountListener.createdCount.get());
           Assert.assertEquals(1, EventCountListener.destroyedCount.get());
           Collection<DistributedObject> distributedObjects = instance.getDistributedObjects();
           Assert.assertTrue(distributedObjects.isEmpty());
         }
       };
   assertTrueEventually(task, 5);
   assertTrueAllTheTime(task, 3);
 }
Example #10
0
  public static void main(String[] args) {
    System.out.println("DEMO Start...");
    Config cfg = new Config();
    HazelcastInstance instance = Hazelcast.newHazelcastInstance(cfg);
    Map<Integer, String> mapCustomers = instance.getMap("customers");
    mapCustomers.put(1, "Joe");
    mapCustomers.put(2, "Ali");
    mapCustomers.put(3, "Avi");

    System.out.println("Customer with key 1: " + mapCustomers.get(1));
    System.out.println("Map Size:" + mapCustomers.size());

    Queue<String> queueCustomers = instance.getQueue("customers");
    queueCustomers.offer("Tom");
    queueCustomers.offer("Mary");
    queueCustomers.offer("Jane");
    System.out.println("First customer: " + queueCustomers.poll());
    System.out.println("Second customer: " + queueCustomers.peek());
    System.out.println("Queue size: " + queueCustomers.size());
  }
Example #11
0
 @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());
 }
 @After
 public void teardown() {
   Hazelcast.shutdownAll();
 }
Example #13
0
public class Node {
  final HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance(null);
  final ITopic<StockPriceUpdate> topicFeed = hazelcast.getTopic("feed");
  final IQueue<Order> qOrders = hazelcast.getQueue("orders");
  final IMap<String, Position> mapPositions =
      hazelcast.getMap("positions"); // <pmId,instrumentId, Position>
  final IMap<Integer, Integer> mapNewOrders = hazelcast.getMap("neworders"); // <pmId, instrumentId>
  final AtomicLong countReceivedStockUpdates = new AtomicLong();
  final AtomicLong countOrdersProcessed = new AtomicLong();
  final AtomicLong countNewOrderEvents = new AtomicLong();
  final AtomicLong countPositionViews = new AtomicLong();
  final Logger logger = Logger.getLogger("Node");
  final ITopic<String> topicLogs = hazelcast.getTopic("logs");
  final ConcurrentMap<Integer, Double> mapStockPrices =
      new ConcurrentHashMap<Integer, Double>(8000);
  final String memberString = hazelcast.getCluster().getLocalMember().toString();
  final int threads = 100;
  final ExecutorService esOrderConsumer = Executors.newFixedThreadPool(threads);
  final ExecutorService esEventProcessor = Executors.newFixedThreadPool(10);
  final ConcurrentMap<Integer, Portfolio> localPMPositions =
      new ConcurrentHashMap<Integer, Portfolio>();
  final ConcurrentMap<Integer, InstrumentInfo> mapInstrumentInfos =
      new ConcurrentHashMap<Integer, InstrumentInfo>();

  public static void main(String[] args) {
    System.setProperty("hazelcast.mc.topic.excludes", "pm.*");
    new Node().init();
  }

  void init() {
    for (int i = 0; i < threads; i++) {
      esOrderConsumer.execute(new PositionQueueSlurper());
    }
    topicFeed.addMessageListener(new StockStreamListener());
    mapNewOrders.addLocalEntryListener(new NewOrderListener());
    startStreamer();
    Executors.newSingleThreadExecutor()
        .execute(
            new Runnable() {
              public void run() {
                while (true) {
                  try {
                    Thread.sleep(5000);
                    long feeds = countReceivedStockUpdates.getAndSet(0) / 5;
                    long orders = countOrdersProcessed.getAndSet(0) / 5;
                    long events = countNewOrderEvents.getAndSet(0) / 5;
                    long views = countPositionViews.getAndSet(0) / 5;
                    log(
                        "Feeds:"
                            + feeds
                            + ", OrdersProcessed:"
                            + orders
                            + ", newOrderEvents:"
                            + events
                            + ", Views:"
                            + views);
                  } catch (Exception e) {
                    e.printStackTrace();
                  }
                }
              }
            });
  }

  void startStreamer() {
    final Timer timer = new Timer();
    timer.scheduleAtFixedRate(
        new TimerTask() {
          @Override
          public void run() {
            for (int i = 0; i < 500; i++) {
              double price = (int) (Math.random() * 50) + 1;
              topicFeed.publish(
                  new StockPriceUpdate(LookupDatabase.randomPickInstrument().id, price));
            }
          }
        },
        0,
        1000);
  }

  class StockStreamListener implements MessageListener<StockPriceUpdate> {
    public void onMessage(final Message<StockPriceUpdate> stockPriceUpdate) {
      esEventProcessor.execute(
          new Runnable() {
            public void run() {
              countReceivedStockUpdates.incrementAndGet();
              int instrumentId = stockPriceUpdate.getMessageObject().getInstrumentId();
              mapStockPrices.put(instrumentId, stockPriceUpdate.getMessageObject().getPrice());
              InstrumentInfo instrumentInfo = createOrGetInstrumentInfo(instrumentId);
              Collection<Portfolio> relatedPortfolios = instrumentInfo.getPortfolios();
              for (Portfolio relatedPortfolio : relatedPortfolios) {
                firePositionViewChanged(createPositionView(relatedPortfolio, instrumentId));
              }
            }
          });
    }
  }

  private void firePositionViewChanged(PositionView positionView) {
    if (positionView == null) return;
    countPositionViews.incrementAndGet();
    ITopic topicPM = hazelcast.getTopic("pm_" + positionView.pmId);
    topicPM.publish(positionView);
  }

  public PositionView createPositionView(Portfolio portfolio, Integer instrumentId) {
    Double lastPrice = mapStockPrices.get(instrumentId);
    if (lastPrice == null) return null;
    Position position = portfolio.getPosition(instrumentId);
    return new PositionView(
        portfolio.pmId,
        instrumentId,
        position.quantity,
        lastPrice,
        position.calculateProfitLoss(lastPrice));
  }

  private Portfolio createOrGetPortfolio(int pmId) {
    Portfolio portfolio = localPMPositions.get(pmId);
    if (portfolio == null) {
      portfolio = new Portfolio(pmId);
      Portfolio existing = localPMPositions.putIfAbsent(pmId, portfolio);
      if (existing != null) {
        portfolio = existing;
      }
    }
    return portfolio;
  }

  private InstrumentInfo createOrGetInstrumentInfo(int instrumentId) {
    InstrumentInfo instrumentInfo = mapInstrumentInfos.get(instrumentId);
    if (instrumentInfo == null) {
      instrumentInfo = new InstrumentInfo();
      InstrumentInfo existing = mapInstrumentInfos.putIfAbsent(instrumentId, instrumentInfo);
      if (existing != null) {
        instrumentInfo = existing;
      }
    }
    return instrumentInfo;
  }

  class NewOrderListener implements EntryListener<Integer, Integer> {
    public void entryAdded(final EntryEvent<Integer, Integer> entryEvent) {
      countNewOrderEvents.incrementAndGet();
      esEventProcessor.execute(
          new Runnable() {
            public void run() {
              Integer pmId = entryEvent.getKey();
              Integer instrumentId = entryEvent.getValue();
              // load all positions for this pm
              // and create the Portfolio
            }
          });
    }

    public void entryRemoved(final EntryEvent<Integer, Integer> entryEvent) {}

    public void entryUpdated(final EntryEvent<Integer, Integer> entryEvent) {
      countNewOrderEvents.incrementAndGet();
      esEventProcessor.execute(
          new Runnable() {
            public void run() {
              try {
                Integer pmId = entryEvent.getKey();
                Integer instrumentId = entryEvent.getValue();
                Position position = mapPositions.get(pmId + "," + instrumentId);
                if (position != null) {
                  Portfolio portfolio = createOrGetPortfolio(pmId);
                  InstrumentInfo instrumentInfo = createOrGetInstrumentInfo(instrumentId);
                  instrumentInfo.addPortfolio(portfolio);
                  portfolio.update(position);
                  PositionView positionView = createPositionView(portfolio, instrumentId);
                  firePositionViewChanged(positionView);
                }
              } catch (Throwable e) {
                e.printStackTrace();
              }
            }
          });
    }

    public void entryEvicted(final EntryEvent<Integer, Integer> entryEvent) {}
  }

  void log(String msg) {
    if (msg != null) {
      logger.info(msg);
      topicLogs.publish(memberString + ": " + msg);
    }
  }

  public class PositionQueueSlurper implements Runnable {

    public void run() {
      while (true) {
        TransactionContext context = hazelcast.newTransactionContext();
        context.beginTransaction();
        Set<Integer> setAllInvolvedPMs = new HashSet<Integer>(9);
        try {
          Order order = qOrders.take();
          countOrdersProcessed.incrementAndGet();
          List<Integer> lsAccounts = order.lsAccounts;
          int accountQuantity = order.quantity / lsAccounts.size();
          //                    for (Integer account : lsAccounts) {
          //                        String key = account + "," + order.instrumentId;
          //                        updatePosition(key, order, account, accountQuantity);
          //                    }
          String key = order.portfolioManagerId + "," + order.instrumentId;
          updatePosition(key, order, order.portfolioManagerId, order.quantity);
          context.commitTransaction();
          //                    setAllInvolvedPMs.addAll(lsAccounts);
          setAllInvolvedPMs.add(order.portfolioManagerId);
          for (Integer involvedPM : setAllInvolvedPMs) {
            mapNewOrders.put(involvedPM, order.instrumentId);
          }
        } catch (Throwable t) {
          t.printStackTrace();
          context.rollbackTransaction();
        }
      }
    }

    public void updatePosition(String key, Order order, int pmId, int quantity) {
      Deal deal = new Deal(quantity, order.price);
      Position position = mapPositions.get(key);
      if (position == null) {
        position = new Position(order.instrumentId);
      } else if (position.getDealSize() > 100) {
        for (int i = 0; i < 10; i++) {
          position.lsDeals.remove(0);
        }
      }
      position.addDeal(deal);
      mapPositions.put(key, position);
    }
  }
}
Example #14
0
 public static void main(String[] args) throws Exception {
   TestApp testApp = new TestApp(Hazelcast.newHazelcastInstance(null));
   testApp.start(args);
 }
Example #15
0
 @After
 public void cleanup() throws Exception {
   Hazelcast.shutdownAll();
 }
 @BeforeClass
 @AfterClass
 public static void init() throws Exception {
   System.setProperty(GroupProperties.PROP_VERSION_CHECK_ENABLED, "false");
   Hazelcast.shutdownAll();
 }
 @AfterClass
 public static void destroy() {
   client.shutdown();
   Hazelcast.shutdownAll();
 }
Example #18
0
 @Test
 public void testMigrationOfTTLAndLock() throws Exception {
   Config config = new Config();
   final HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config);
   final HazelcastInstance h2 = Hazelcast.newHazelcastInstance(config);
   ConcurrentMapManager cmm1 = getConcurrentMapManager(h1);
   ConcurrentMapManager cmm2 = getConcurrentMapManager(h2);
   final IMap imap1 = h1.getMap("default");
   final IMap imap2 = h2.getMap("default");
   final Data dKey = toData("1");
   assertTrue(migrateKey("1", h1, h1, 0));
   assertTrue(migrateKey("1", h1, h2, 1));
   imap1.put("1", "value1", 60, TimeUnit.SECONDS);
   imap1.lock("1");
   Future put2 = imap2.putAsync("1", "value2");
   imap2.addEntryListener(
       new EntryAdapter() {
         @Override
         public void entryUpdated(EntryEvent entryEvent) {
           System.out.println(entryEvent);
         }
       },
       "1",
       true);
   if (put2 == null) fail();
   Thread.sleep(1000);
   CMap cmap1 = getCMap(h1, "default");
   CMap cmap2 = getCMap(h2, "default");
   Record record1 = cmap1.getRecord(dKey);
   assertEquals(1, record1.getScheduledActionCount());
   for (ScheduledAction scheduledAction : record1.getScheduledActions()) {
     assertTrue(scheduledAction.isValid());
   }
   assertNotNull(record1.getListeners());
   assertEquals(1, record1.getListeners().size());
   DistributedLock lock = cmap1.getRecord(dKey).getLock();
   assertTrue(cmm1.thisAddress.equals(lock.getLockAddress()));
   assertTrue(lock.getLockThreadId() != -1);
   assertEquals(1, lock.getLockCount());
   assertEquals(1, cmap1.mapRecords.size());
   assertEquals(1, cmap2.mapRecords.size());
   assertEquals(0, cmap2.getMapIndexService().getOwnedRecords().size());
   assertEquals(1, cmap1.getMapIndexService().getOwnedRecords().size());
   assertTrue(migrateKey("1", h1, h2, 0));
   assertTrue(migrateKey("1", h1, h1, 1));
   assertEquals(1, cmap1.mapRecords.size());
   assertEquals(1, cmap2.mapRecords.size());
   assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size());
   assertEquals(1, cmap2.getMapIndexService().getOwnedRecords().size());
   assertTrue(cmap1.getRecord(dKey).getRemainingTTL() < 60000);
   assertTrue(cmap2.getRecord(dKey).getRemainingTTL() < 60000);
   Record record2 = cmap2.getRecord(dKey);
   lock = record2.getLock();
   assertTrue(cmm1.thisAddress.equals(lock.getLockAddress()));
   assertTrue(lock.getLockThreadId() != -1);
   assertEquals(1, lock.getLockCount());
   lock = cmap1.getRecord(dKey).getLock();
   assertTrue(cmm1.thisAddress.equals(lock.getLockAddress()));
   assertTrue(lock.getLockThreadId() != -1);
   assertEquals(1, lock.getLockCount());
   imap1.unlock("1");
   put2.get(10, TimeUnit.SECONDS);
   assertEquals("value2", imap1.get("1"));
   assertEquals("value2", imap2.get("1"));
   assertNotNull(record2.getListeners());
   assertEquals(1, record2.getListeners().size());
 }
 @AfterClass
 public static void destroy() {
   hz.getLifecycleService().shutdown();
   Hazelcast.shutdownAll();
 }
 @BeforeClass
 public static void init() {
   server = Hazelcast.newHazelcastInstance();
   hz = HazelcastClient.newHazelcastClient(null);
   s = hz.getSemaphore(name);
 }
 @BeforeClass
 @AfterClass
 public static void start() {
   Hazelcast.shutdownAll();
 }
 @After
 @Before
 public void cleanup() throws Exception {
   Hazelcast.shutdownAll();
   HazelcastClient.shutdownAll();
 }
Example #23
0
 @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();
 }
  public BaseHazelCastStateTracker(String connectionString, String type, int stateTrackerPort)
      throws Exception {
    log.info(
        "Setting up hazelcast with type "
            + type
            + " connection string "
            + connectionString
            + " and port "
            + stateTrackerPort);

    if (type.equals("master") && !PortTaken.portTaken(stateTrackerPort)) {
      // sets up a proper connection string for reference wrt external actors needing a reference
      if (connectionString.equals("master")) {
        String host = InetAddress.getLocalHost().getHostName();
        this.connectionString = host + ":" + stateTrackerPort;
      }

      this.hazelCastPort = stateTrackerPort;
      config = hazelcast();

      h = Hazelcast.newHazelcastInstance(config);
      h.getCluster()
          .addMembershipListener(
              new MembershipListener() {

                @Override
                public void memberAdded(MembershipEvent membershipEvent) {
                  log.info("Member added " + membershipEvent.toString());
                }

                @Override
                public void memberRemoved(MembershipEvent membershipEvent) {
                  log.info("Member removed " + membershipEvent.toString());
                }

                @Override
                public void memberAttributeChanged(MemberAttributeEvent memberAttributeEvent) {
                  log.info("Member changed " + memberAttributeEvent.toString());
                }
              });
    } else if (type.equals("master") && PortTaken.portTaken(stateTrackerPort))
      throw new IllegalStateException(
          "Specified type was master and the port specified was taken, please specify a different port");
    else {

      setConnectionString(connectionString);
      log.info("Connecting to hazelcast on " + connectionString);
      ClientConfig client = new ClientConfig();
      client.getNetworkConfig().addAddress(connectionString);
      h = HazelcastClient.newHazelcastClient(client);
    }

    this.type = type;

    jobs = h.getList(JOBS);
    workers = h.getList(WORKERS);

    // we can make the assumption workers isn't empty because
    // the master node by default comes with a applyTransformToDestination of workers
    if (!this.type.equals("master")) {
      while (workers.isEmpty()) {
        log.warn("Waiting for data sync...");
        Thread.sleep(1000);
      }

      log.info("Workers is " + workers.size());
    }

    begunTraining = h.getAtomicReference(BEGUN);
    miniBatchSize = h.getAtomicReference(INPUT_SPLIT);
    workerEnabled = h.getMap(WORKER_ENABLED);
    replicate = h.getList(REPLICATE_WEIGHTS);
    topics = h.getList(TOPICS);
    updates = h.getList(UPDATES);
    heartbeat = h.getMap(HEART_BEAT);
    master = h.getAtomicReference(RESULT);
    isPretrain = h.getAtomicReference(IS_PRETRAIN);
    numTimesPretrain = h.getAtomicReference(NUM_TIMES_RUN_PRETRAIN);
    numTimesPretrainRan = h.getAtomicReference(NUM_TIMES_PRETRAIN_RAN);
    done = h.getAtomicReference(DONE);
    validationEpochs = h.getAtomicReference(VALIDATION_EPOCHS);
    improvementThreshold = h.getAtomicReference(IMPROVEMENT_THRESHOLD);
    bestLoss = h.getAtomicReference(BEST_LOSS);
    earlyStop = h.getAtomicReference(EARLY_STOP);
    patience = h.getAtomicReference(PATIENCE);
    patienceIncrease = h.getAtomicReference(PATIENCE_INCREASE);
    numBatches = h.getAtomicReference(NUM_BATCHES_SO_FAR_RAN);

    // applyTransformToDestination defaults only when master, otherwise, overrides previous values
    if (type.equals("master")) {
      begunTraining.set(false);
      saver = createUpdateSaver();
      numTimesPretrainRan.set(0);
      numTimesPretrain.set(1);
      isPretrain.set(true);
      done.set(false);
      resource = new StateTrackerDropWizardResource(this);
      bestLoss.set(Double.POSITIVE_INFINITY);
      earlyStop.set(true);
      patience.set(40.0);
      patienceIncrease.set(2.0);
      improvementThreshold.set(0.995);
      validationEpochs.set((int) Math.min(10, patience() / 2));
      numBatches.set(0);
    }
  }