Exemplo n.º 1
0
  @Test
  public void testListener() throws Exception {
    final int maxItems = 10;
    final CountDownLatch itemAddedLatch = new CountDownLatch(maxItems);
    final CountDownLatch itemRemovedLatch = new CountDownLatch(maxItems);

    final IQueue queue = client.getQueue(randomString());

    String id =
        queue.addItemListener(
            new ItemListener() {

              public void itemAdded(ItemEvent itemEvent) {
                itemAddedLatch.countDown();
              }

              public void itemRemoved(ItemEvent item) {
                itemRemovedLatch.countDown();
              }
            },
            true);

    new Thread() {
      public void run() {
        for (int i = 0; i < maxItems; i++) {
          queue.offer(i);
          queue.remove(i);
        }
      }
    }.start();

    assertTrue(itemAddedLatch.await(5, TimeUnit.SECONDS));
    assertTrue(itemRemovedLatch.await(5, TimeUnit.SECONDS));
    queue.removeItemListener(id);
  }
Exemplo n.º 2
0
  @Test
  public void testOfferPoll() throws IOException, InterruptedException {

    final IQueue q = client.getQueue(queueForTestOfferPoll);

    for (int i = 0; i < 10; i++) {
      boolean result = q.offer("item");
      if (i < maxSizeForQueue) {
        assertTrue(result);
      } else {
        assertFalse(result);
      }
    }
    assertEquals(maxSizeForQueue, q.size());

    final Thread t1 =
        new Thread() {
          public void run() {
            try {
              Thread.sleep(2 * 1000);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
            q.poll();
          }
        };
    t1.start();

    boolean result = q.offer("item", 5, TimeUnit.SECONDS);
    assertTrue(result);

    for (int i = 0; i < 10; i++) {
      Object o = q.poll();
      if (i < maxSizeForQueue) {
        assertNotNull(o);
      } else {
        assertNull(o);
      }
    }
    assertEquals(0, q.size());

    final Thread t2 =
        new Thread() {
          public void run() {
            try {
              Thread.sleep(2 * 1000);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
            q.offer("item1");
          }
        };
    t2.start();

    Object o = q.poll(5, TimeUnit.SECONDS);
    assertEquals("item1", o);
    t1.join(10000);
    t2.join(10000);
  }
Exemplo n.º 3
0
  @Test
  public void testContains() {
    final IQueue q = client.getQueue(randomString());

    q.offer(1);
    assertTrue(q.contains(1));
    assertFalse(q.contains(2));
  }
Exemplo n.º 4
0
  @Test
  public void testRemove() throws IOException {
    final IQueue q = client.getQueue(randomString());

    q.offer(1);
    assertTrue(q.remove(1));
    assertFalse(q.remove(2));
  }
Exemplo n.º 5
0
  @Test
  public void testRemainingCapacity() throws IOException {
    final IQueue q = client.getQueue(randomString());

    assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
    q.offer("one");
    assertEquals(Integer.MAX_VALUE - 1, q.remainingCapacity());
  }
Exemplo n.º 6
0
 @Test
 public void testPeak() throws InterruptedException {
   final IQueue q = client.getQueue(randomString());
   q.offer(1);
   assertEquals(1, q.peek());
   assertEquals(1, q.peek());
   assertEquals(1, q.size());
 }
Exemplo n.º 7
0
  @Test
  public void testQueueWithSizeLimit() {
    final IQueue q = client.getQueue(queueForTestQueueWithSizeLimit);

    for (int i = 0; i < maxSizeForQueue; i++) {
      q.offer(i);
    }
    assertFalse(q.offer(maxSizeForQueue));
  }
Exemplo n.º 8
0
  @Test
  public void testSize() {
    final int maxItems = 143;
    final IQueue q = client.getQueue(randomString());

    for (int i = 0; i < maxItems; i++) {
      q.add(i);
    }
    assertEquals(maxItems, q.size());
  }
Exemplo n.º 9
0
  @Test
  public void testRetainEmptyList() throws IOException {
    final int maxItems = 131;
    final IQueue q = client.getQueue(randomString());

    for (int i = 0; i < maxItems; i++) {
      q.add(i);
    }

    List retain = new LinkedList();
    assertTrue(q.retainAll(retain));
    assertEquals(0, q.size());
  }
Exemplo n.º 10
0
  @Test
  public void testToArray() {
    final int maxItems = 19;
    final IQueue q = client.getQueue(randomString());

    Object[] offered = new Object[maxItems];
    for (int i = 0; i < maxItems; i++) {
      q.offer(i);
      offered[i] = i;
    }

    Object[] result = q.toArray();
    assertEquals(offered, result);
  }
Exemplo n.º 11
0
  @Test
  public void testIterator() {
    final int maxItems = 18;
    final IQueue q = client.getQueue(randomString());

    for (int i = 0; i < maxItems; i++) {
      q.offer(i);
    }

    int i = 0;
    for (Object o : q) {
      assertEquals(i++, o);
    }
  }
Exemplo n.º 12
0
  @Test
  public void testAddAll() throws IOException {
    final int maxItems = 13;
    final IQueue q = client.getQueue(randomString());

    Collection coll = new ArrayList(maxItems);

    for (int i = 0; i < maxItems; i++) {
      coll.add(i);
    }

    assertTrue(q.addAll(coll));
    assertEquals(coll.size(), q.size());
  }
Exemplo n.º 13
0
  @Test
  public void testContainsAll() {
    final int maxItems = 11;
    final IQueue q = client.getQueue(randomString());

    List trueList = new ArrayList();
    List falseList = new ArrayList();
    for (int i = 0; i < maxItems; i++) {
      q.offer(i);
      trueList.add(i);
      falseList.add(i + 1);
    }
    assertTrue(q.containsAll(trueList));
    assertFalse(q.containsAll(falseList));
  }
Exemplo n.º 14
0
  @Test
  public void testRemoveList_whereNotFound() throws IOException {
    final int maxItems = 131;
    final IQueue q = client.getQueue(randomString());

    List removeList = new LinkedList();
    for (int i = 0; i < maxItems; i++) {
      q.add(i);
    }
    removeList.add(maxItems + 1);
    removeList.add(maxItems + 2);

    assertFalse(q.removeAll(removeList));
    assertEquals(maxItems, q.size());
  }
Exemplo n.º 15
0
  @Test
  public void testDrain() {
    final int maxItems = 12;
    final IQueue q = client.getQueue(randomString());

    List offeredList = new LinkedList();
    for (int i = 0; i < maxItems; i++) {
      q.offer(i);
      offeredList.add(i);
    }
    List drainedList = new LinkedList();
    int totalDrained = q.drainTo(drainedList);

    assertEquals(maxItems, totalDrained);
    assertEquals(offeredList, drainedList);
  }
Exemplo n.º 16
0
  @Test
  public void testPartialDrain() {
    final int maxItems = 15;
    final int itemsToDrain = maxItems / 2;

    final IQueue q = client.getQueue(randomString());

    List expectedList = new LinkedList();
    for (int i = 0; i < maxItems; i++) {
      q.offer(i);
      if (i < itemsToDrain) {
        expectedList.add(i);
      }
    }
    List drainedList = new LinkedList();
    int totalDrained = q.drainTo(drainedList, itemsToDrain);

    assertEquals(itemsToDrain, totalDrained);
    assertEquals(expectedList, drainedList);
  }
Exemplo n.º 17
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);
    }
  }
}
Exemplo n.º 18
0
 public IQueue<Object> getQueue() {
   //        if (queue == null) {
   queue = hazelcast.getQueue(namespace);
   //        }
   return queue;
 }
Exemplo n.º 19
0
 @Test
 public void testEmptyPeak() throws InterruptedException {
   final IQueue q = client.getQueue(randomString());
   assertNull(q.peek());
 }
Exemplo n.º 20
0
 @Test
 public void testTake() throws InterruptedException {
   final IQueue q = client.getQueue(randomString());
   q.put(1);
   assertEquals(1, q.take());
 }
Exemplo n.º 21
0
 @Test
 public void testadd() {
   final IQueue q = client.getQueue(randomString());
   assertTrue(q.add(1));
   assertEquals(1, q.size());
 }
Exemplo n.º 22
0
 @Test
 public void testOfferWithTimeOut() throws IOException, InterruptedException {
   final IQueue q = client.getQueue(randomString());
   boolean result = q.offer(1, 50, TimeUnit.MILLISECONDS);
   assertTrue(result);
 }
Exemplo n.º 23
0
 @Test(expected = NoSuchElementException.class)
 public void testEmptyRemove() throws IOException {
   final IQueue q = client.getQueue(randomString());
   q.remove();
 }
Exemplo n.º 24
0
 @Test(expected = UnsupportedOperationException.class)
 public void testGetLocalQueueStats() {
   final IQueue q = client.getQueue(randomString());
   q.getLocalQueueStats();
 }
Exemplo n.º 25
0
 @Test
 public void testRemoveTop() throws IOException, InterruptedException {
   final IQueue q = client.getQueue(randomString());
   q.offer(1);
   assertEquals(1, q.remove());
 }
Exemplo n.º 26
0
 @Test
 public void testNotIsEmpty() {
   final IQueue q = client.getQueue(randomString());
   q.offer(1);
   assertFalse(q.isEmpty());
 }
Exemplo n.º 27
0
 @Test
 public void testIsEmpty() {
   final IQueue q = client.getQueue(randomString());
   assertTrue(q.isEmpty());
 }
Exemplo n.º 28
0
 @Test(expected = NoSuchElementException.class)
 public void testEmptyElement() throws InterruptedException {
   final IQueue q = client.getQueue(randomString());
   q.element();
 }