com.hazelcast.map.IMap Java Examples

The following examples show how to use com.hazelcast.map.IMap. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TopicManager.java    From mercury with Apache License 2.0 6 votes vote down vote up
private void createTopic(String topic) {
    if (!topicExists(topic)) {
        String now = Utility.getInstance().date2str(new Date(), true);
        String nodes = HazelcastSetup.NAMESPACE+NODES;
        String realTopic = HazelcastSetup.NAMESPACE+topic;
        HazelcastInstance client = HazelcastSetup.getHazelcastClient();
        IMap<String, byte[]> map = client.getMap(nodes);
        Map<String, String> metadata = new HashMap<>();
        metadata.put("node", topic);
        metadata.put("name", Platform.getInstance().getName());
        metadata.put("created", now);
        metadata.put("updated", now);
        try {
            map.put(topic, msgPack.pack(metadata));
            // create topic if not exists
            client.getReliableTopic(realTopic);
            log.info("Topic {} created", realTopic);
        } catch (IOException e) {
            // this does not happen
            log.error("Unable to create topic {} - {}", topic, e.getMessage());
        }
    }
}
 
Example #2
Source File: TopicManager.java    From mercury with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private Map<String, String> getTopicsWithTimestamp() throws IOException {
    String nodes = HazelcastSetup.NAMESPACE+NODES;
    HazelcastInstance client = HazelcastSetup.getHazelcastClient();
    IMap<String, byte[]> map = client.getMap(nodes);
    Map<String, String> result = new HashMap<>();
    String namespace = Platform.getInstance().getNamespace();
    for (String t: map.keySet()) {
        // skip topics that are not in this namespace
        if (namespace != null && !t.endsWith(namespace)) {
            continue;
        }
        Map<String, String> metadata = (Map<String, String>) msgPack.unpack(map.get(t));
        if (metadata.containsKey("updated")) {
            result.put(t, metadata.get("updated"));
        }
    }
    return result;
}
 
Example #3
Source File: TopicManager.java    From mercury with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private List<Map<String, String>> getTopics() throws IOException {
    List<Map<String, String>> result = new ArrayList<>();
    String nodes = HazelcastSetup.NAMESPACE+NODES;
    HazelcastInstance client = HazelcastSetup.getHazelcastClient();
    IMap<String, byte[]> map = client.getMap(nodes);
    String namespace = Platform.getInstance().getNamespace();
    for (String t: map.keySet()) {
        // skip topics that are not in this namespace
        if (namespace != null && !t.endsWith(namespace)) {
            continue;
        }
        Map<String, String> metadata = (Map<String, String>) msgPack.unpack(map.get(t));
        result.add(metadata);
    }
    return result;
}
 
Example #4
Source File: ClientManager.java    From openmeetings with Apache License 2.0 6 votes vote down vote up
/**
 * This method will return count of users in room _after_ adding
 *
 * @param c - client to be added to the room
 * @return count of users in room _after_ adding
 */
public int addToRoom(Client c) {
	Room r = c.getRoom();
	Long roomId = r.getId();
	confLogDao.add(
			ConferenceLog.Type.ROOM_ENTER
			, c.getUserId(), "0", roomId
			, c.getRemoteAddress()
			, String.valueOf(roomId));
	log.debug("Adding online room client: {}, room: {}", c.getUid(), roomId);
	IMap<Long, Set<String>> rooms = rooms();
	rooms.lock(roomId);
	rooms.putIfAbsent(roomId, ConcurrentHashMap.newKeySet());
	Set<String> set = rooms.get(roomId);
	set.add(c.getUid());
	final int count = set.size();
	rooms.put(roomId, set);
	onlineRooms.put(roomId, set);
	rooms.unlock(roomId);
	String serverId = c.getServerId();
	addRoomToServer(serverId, r);
	update(c);
	return count;
}
 
Example #5
Source File: TopicManager.java    From mercury with Apache License 2.0 6 votes vote down vote up
private List<String> listTopics() {
    List<String> result = new ArrayList<>();
    String nodes = HazelcastSetup.NAMESPACE+NODES;
    HazelcastInstance client = HazelcastSetup.getHazelcastClient();
    IMap<String, byte[]> map = client.getMap(nodes);
    String namespace = Platform.getInstance().getNamespace();
    for (String t: map.keySet()) {
        // skip topics that are not in this namespace
        if (namespace != null && !t.endsWith(namespace)) {
            continue;
        }
        if (regularTopicFormat(t)) {
            result.add(t);
        }
    }
    return result;
}
 
Example #6
Source File: JetRunner.java    From beam with Apache License 2.0 6 votes vote down vote up
private JetPipelineResult run(DAG dag) {
  startClusterIfNeeded(options);

  JetInstance jet =
      getJetInstance(
          options); // todo: we use single client for each job, it might be better to have a
  // shared client with refcount

  Job job = jet.newJob(dag, getJobConfig(options));
  IMap<String, MetricUpdates> metricsAccumulator =
      jet.getMap(JetMetricsContainer.getMetricsMapName(job.getId()));
  JetPipelineResult pipelineResult = new JetPipelineResult(job, metricsAccumulator);
  CompletableFuture<Void> completionFuture =
      job.getFuture()
          .whenCompleteAsync(
              (r, f) -> {
                pipelineResult.freeze(f);
                metricsAccumulator.destroy();
                jet.shutdown();

                stopClusterIfNeeded(options);
              });
  pipelineResult.setCompletionFuture(completionFuture);

  return pipelineResult;
}
 
Example #7
Source File: HazelcastLockProvider.java    From ShedLock with Apache License 2.0 6 votes vote down vote up
@Override
@NonNull
public Optional<SimpleLock> lock(@NonNull LockConfiguration lockConfiguration) {
    log.trace("lock - Attempt : {}", lockConfiguration);
    final Instant now = ClockProvider.now();
    final String lockName = lockConfiguration.getName();
    final IMap<String, HazelcastLock> store = getStore();
    try {
        // lock the map key entry
        store.lock(lockName, keyLockTime(lockConfiguration), TimeUnit.MILLISECONDS);
        // just one thread at a time, in the cluster, can run this code
        // each thread waits until the lock to be unlock
        if (tryLock(lockConfiguration, now)) {
            return Optional.of(new HazelcastSimpleLock(this, lockConfiguration));
        }
    } finally {
        // released the map lock for the others threads
        store.unlock(lockName);
    }
    return Optional.empty();
}
 
Example #8
Source File: RandomSimulator.java    From hazelcast-jet-demos with Apache License 2.0 6 votes vote down vote up
/**
 * Set up an event to hang the bets off  
 */
public default void createFutureEvent() {
    // Grab some horses to use as runners in races
    final IMap<Horse, Object> fromHC = getClient().getMap("winners");
    final Set<Horse> horses = fromHC.keySet();

    // Now set up some future-dated events for next Sat
    final LocalDate nextSat = LocalDate.now().with(TemporalAdjusters.next(DayOfWeek.SATURDAY));
    LocalTime raceTime = LocalTime.of(11, 0); // 1100 start
    final Event e = CentralFactory.eventOf("Racing from Epsom", nextSat);
    final Set<Horse> runners = makeRunners(horses, 10);
    for (int i = 0; i < 18; i++) {
        final Map<Horse, Double> runnersWithOdds = makeSimulatedOdds(runners);
        final Race r = CentralFactory.raceOf(LocalDateTime.of(nextSat, raceTime), runnersWithOdds);
        e.addRace(r);

        raceTime = raceTime.plusMinutes(10);
    }
    final IMap<Long, Event> events = getClient().getMap("events");
    events.put(e.getID(), e);
}
 
Example #9
Source File: JetBetMain.java    From hazelcast-jet-demos with Apache License 2.0 6 votes vote down vote up
/**
 * Helper method that calculates largest possible loss and the results that
 * caused that outcome.
 */
public void outputPossibleLosses() {
    final IMap<Race, Map<Horse, Double>> risks = jet.getHazelcastInstance().getMap(WORST_ID);

    final Double apocalypse = risks.entrySet().stream()
                                   .map(e -> tuple2(e.getKey(), getMaxExposureAsTuple(e.getValue())))
                                   .sorted(Comparator.comparing(t -> t.f1().f1()))
                                   .limit(20)

                                   // Output "perfect storm" combination of top 20 results that caused the losses
                                   .peek(t -> System.out.println("Horse: " + t.f1().f0().getName() + " ; Losses: " + t.f1().f1()))

                                   // Finally output the maximum possible loss
                                   .map(tr -> tr.f1())
                                   .map(Entry<Horse, Double>::getValue)
                                   .reduce(0.0, (ra, rb) -> ra + rb);

    System.out.println("Worst case total losses: " + apocalypse);
}
 
Example #10
Source File: TestJetMain.java    From hazelcast-jet-demos with Apache License 2.0 6 votes vote down vote up
@Test
public void testBuildDAG() {
    User u = makeUser();
    Bet b = makeBet();
    u.addBet(b);
    assertNotNull(b);
    try {
        IMap<String, ?> ism = jet.getMap(WORST_ID);
        System.out.println(ism);
        System.out.println("Size: " + ism.size());
        for (String s : ism.keySet()) {
            System.out.println(s + " : " + ism.get(s));
        }
    } finally {
        Jet.shutdownAll();
    }
}
 
Example #11
Source File: CryptoSentimentGui.java    From hazelcast-jet-demos with Apache License 2.0 6 votes vote down vote up
public CryptoSentimentGui(
        IMap<Tuple2<CoinType, WinSize>, Tuple2<Double, Long>> jetResults
) {
    for (WinSize winSize : WinSize.values()) {
        for (CoinType coinType : CoinType.values()) {
            sentimentDataset.addValue(0.0, coinType, winSize);
            mentionsDataset.addValue(0.0, coinType, winSize);
        }
    }

    jetResults.addEntryListener(
            (EntryAddedListener<Tuple2<CoinType, WinSize>, Tuple2<Double, Long>>) this::onMapEvent, true);
    jetResults.addEntryListener(
            (EntryUpdatedListener<Tuple2<CoinType, WinSize>, Tuple2<Double, Long>>) this::onMapEvent, true);
    EventQueue.invokeLater(this::startGui);
}
 
Example #12
Source File: ModelServerClassification.java    From hazelcast-jet-demos with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    System.setProperty("hazelcast.logging.type", "log4j");

    if (args.length != 2) {
        System.out.println("Usage: ModelServerClassification <data path> <model server address>");
        System.exit(1);
    }
    String dataPath = args[0];
    String serverAddress = args[1];

    JobConfig jobConfig = new JobConfig();
    jobConfig.attachDirectory(dataPath, "data");

    JetInstance instance = Jet.newJetInstance();
    try {
        IMap<Long, String> reviewsMap = instance.getMap("reviewsMap");
        SampleReviews.populateReviewsMap(reviewsMap);

        Pipeline p = buildPipeline(serverAddress, reviewsMap);

        instance.newJob(p, jobConfig).join();
    } finally {
        instance.shutdown();
    }
}
 
Example #13
Source File: InProcessClassification.java    From hazelcast-jet-demos with Apache License 2.0 6 votes vote down vote up
private static Pipeline buildPipeline(IMap<Long, String> reviewsMap) {
    // Set up the mapping context that loads the model on each member, shared
    // by all parallel processors on that member.
    ServiceFactory<Tuple2<SavedModelBundle, WordIndex>, Tuple2<SavedModelBundle, WordIndex>> modelContext = ServiceFactory
            .withCreateContextFn(context -> {
                File data = context.attachedDirectory("data");
                SavedModelBundle bundle = SavedModelBundle.load(data.toPath().resolve("model/1").toString(), "serve");
                return tuple2(bundle, new WordIndex(data));
            })
            .withDestroyContextFn(t -> t.f0().close())
            .withCreateServiceFn((context, tuple2) -> tuple2);
    Pipeline p = Pipeline.create();
    p.readFrom(Sources.map(reviewsMap))
     .map(Map.Entry::getValue)
     .mapUsingService(modelContext, (tuple, review) -> classify(review, tuple.f0(), tuple.f1()))
     // TensorFlow executes models in parallel, we'll use 2 local threads to maximize throughput.
     .setLocalParallelism(2)
     .writeTo(Sinks.logger(t -> String.format("Sentiment rating for review \"%s\" is %.2f", t.f0(), t.f1())));
    return p;
}
 
Example #14
Source File: InProcessClassification.java    From hazelcast-jet-demos with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    System.setProperty("hazelcast.logging.type", "log4j");

    if (args.length != 1) {
        System.out.println("Usage: InProcessClassification <data path>");
        System.exit(1);
    }

    String dataPath = args[0];
    JetInstance instance = Jet.newJetInstance();
    JobConfig jobConfig = new JobConfig();
    jobConfig.attachDirectory(dataPath, "data");

    try {
        IMap<Long, String> reviewsMap = instance.getMap("reviewsMap");
        SampleReviews.populateReviewsMap(reviewsMap);
        instance.newJob(buildPipeline(reviewsMap), jobConfig).join();
    } finally {
        instance.shutdown();
    }
}
 
Example #15
Source File: HazelcastCacheMetricsCompatibilityTest.java    From micrometer with Apache License 2.0 6 votes vote down vote up
@Disabled("This only demonstrates why we can't support miss count in Hazelcast.")
@Issue("#586")
@Test
void multiInstanceMissCount() {
    IMap<String, String> cache2 = Hazelcast.newHazelcastInstance(config).getMap("mycache");

    // Since each member owns 1/N (N being the number of members in the cluster) entries of a distributed map,
    // we add two entries so we can deterministically say that each cache will "own" one entry.
    cache.put("k1", "v");
    cache.put("k2", "v");

    cache.get("k1");
    cache.get("k2");

    // cache stats: hits = 1, gets = 2, puts = 2
    // cache2 stats: hits = 1, gets = 0, puts = 0

    assertThat(cache.getLocalMapStats().getHits()).isEqualTo(1);
    assertThat(cache.getLocalMapStats().getGetOperationCount()).isEqualTo(2);
    assertThat(cache2.getLocalMapStats().getHits()).isEqualTo(1);

    // ... and this is why we can't calculate miss count in Hazelcast. sorry!
}
 
Example #16
Source File: TestDataHelper.java    From spring-data-hazelcast with Apache License 2.0 6 votes vote down vote up
@After
public void tearDown() {
    for (String mapName : TestConstants.OSCAR_MAP_NAMES) {
        IMap<String, ?> iMap = this.hazelcastInstance.getMap(mapName);
        iMap.clear();
    }

    checkMapsEmpty("tearDown");

    Collection<DistributedObject> distributedObjects = this.hazelcastInstance.getDistributedObjects();

    for (DistributedObject distributedObject : distributedObjects) {
        assertThat(distributedObject.getName(), distributedObject, instanceOf(IMap.class));
        assertThat(distributedObject.getName(), isIn(TestConstants.OSCAR_MAP_NAMES));
    }

    assertThat("Correct number of distributed objects", distributedObjects.size(),
            equalTo(TestConstants.OSCAR_MAP_NAMES.length));

}
 
Example #17
Source File: Lab4.java    From hazelcast-jet-training with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    JetInstance jet = Jet.bootstrappedInstance();

    // symbol -> company name
    // random symbols from https://www.nasdaq.com
    IMap<String, String> lookupTable = jet.getMap(LOOKUP_TABLE);
    lookupTable.put("AAPL", "Apple Inc. - Common Stock");
    lookupTable.put("GOOGL", "Alphabet Inc.");
    lookupTable.put("MSFT", "Microsoft Corporation");

    Pipeline p = buildPipeline(lookupTable);

    try {
        jet.newJob(p).join();
    } finally {
        jet.shutdown();
    }
}
 
Example #18
Source File: Solution4.java    From hazelcast-jet-training with Apache License 2.0 6 votes vote down vote up
public static void main (String[] args) {
    JetInstance jet = Jet.bootstrappedInstance();

    // symbol -> company name
    IMap<String, String> lookupTable = jet.getMap(LOOKUP_TABLE);
    lookupTable.put("AAPL", "Apple Inc. - Common Stock");
    lookupTable.put("GOOGL", "Alphabet Inc.");
    lookupTable.put("MSFT", "Microsoft Corporation");

    Pipeline p = buildPipeline(lookupTable);
    try {
        jet.newJob(p).join();
    } finally {
        jet.shutdown();
    }
}
 
Example #19
Source File: TestDataHelper.java    From spring-data-hazelcast with Apache License 2.0 5 votes vote down vote up
private void loadSong(IMap<String, Song> songMap) {
    for (int i = 0; i < TestData.bestSongs.length; i++) {
        Song song = new Song();

        song.setId(Integer.toString((int) TestData.bestSongs[i][0]));
        song.setTitle(TestData.bestSongs[i][1].toString());

        songMap.put(song.getId(), song);
    }
}
 
Example #20
Source File: TestDataHelper.java    From spring-data-hazelcast with Apache License 2.0 5 votes vote down vote up
private void loadPerson(IMap<String, Person> personMap) {
    for (int i = 0; i < TestData.bestActors.length; i++) {
        Person person = new Person();

        person.setId(Integer.toString((int) TestData.bestActors[i][0]));
        person.setFirstname(TestData.bestActors[i][1].toString());
        person.setLastname(TestData.bestActors[i][2].toString());

        personMap.put(person.getId(), person);
    }
}
 
Example #21
Source File: TestDataHelper.java    From spring-data-hazelcast with Apache License 2.0 5 votes vote down vote up
private void loadMakeup(IMap<String, Makeup> akeupMap) {
    for (int i = 0; i < TestData.bestMakeUp.length; i++) {
        Makeup makeup = new Makeup();

        makeup.setId(Integer.toString((int) TestData.bestMakeUp[i][0]));
        makeup.setFilmTitle(TestData.bestMakeUp[i][1].toString());
        makeup.setArtistOrArtists(TestData.bestMakeUp[i][2].toString());

        makeupMap.put(makeup.getId(), makeup);
    }
}
 
Example #22
Source File: JetMetricResults.java    From beam with Apache License 2.0 5 votes vote down vote up
private synchronized void updateLocalMetrics(IMap<String, MetricUpdates> metricsAccumulator) {
  counters.clear();
  distributions.clear();
  gauges.clear();

  for (MetricUpdates metricUpdates : metricsAccumulator.values()) {
    counters.merge(metricUpdates.counterUpdates());
    distributions.merge(metricUpdates.distributionUpdates());
    gauges.merge(metricUpdates.gaugeUpdates());
  }
}
 
Example #23
Source File: TestDataHelper.java    From spring-data-hazelcast with Apache License 2.0 5 votes vote down vote up
private void loadMovie(IMap<String, Movie> movieMap) {
    for (int i = 0; i < TestData.bestPictures.length; i++) {
        Movie movie = new Movie();

        movie.setId(Integer.toString((int) TestData.bestPictures[i][0]));
        movie.setTitle(TestData.bestPictures[i][1].toString());

        movieMap.put(movie.getId(), movie);
    }
}
 
Example #24
Source File: TestDataHelper.java    From spring-data-hazelcast with Apache License 2.0 5 votes vote down vote up
protected void checkMapsEmpty(String phase) {
    for (String mapName : TestConstants.OSCAR_MAP_NAMES) {
        IMap<String, ?> iMap = this.hazelcastInstance.getMap(mapName);
        assertThat(phase + "(): No test data left behind by previous tests in '" + iMap.getName() + "'", iMap.size(),
                equalTo(0));
    }
}
 
Example #25
Source File: QuickPollManager.java    From openmeetings with Apache License 2.0 5 votes vote down vote up
public void start(Client c) {
	Long roomId = c.getRoomId();
	if (!c.hasRight(Room.Right.PRESENTER) || isStarted(roomId)) {
		return;
	}
	log.debug("Starting quick poll, room: {}", roomId);
	IMap<Long, Map<Long, Boolean>> polls = map();
	polls.lock(roomId);
	polls.putIfAbsent(roomId, new ConcurrentHashMap<Long, Boolean>());
	polls.unlock(roomId);
	WebSocketHelper.sendRoom(new TextRoomMessage(roomId, c, Type.QUICK_POLL_UPDATED, c.getUid()));
}
 
Example #26
Source File: BackupExpirationMapTest.java    From hazelcast-simulator with Apache License 2.0 5 votes vote down vote up
private static long totalEntryCountOnNode(String name, HazelcastInstance instance) {
    IMap map = instance.getMap(name);
    LocalMapStats localMapStats = map.getLocalMapStats();
    long ownedEntryCount = localMapStats.getOwnedEntryCount();
    long backupEntryCount = localMapStats.getBackupEntryCount();
    return ownedEntryCount + backupEntryCount;
}
 
Example #27
Source File: Lab4.java    From hazelcast-jet-training with Apache License 2.0 5 votes vote down vote up
private static Pipeline buildPipeline(IMap<String, String> lookupTable) {
    Pipeline p = Pipeline.create();

    p.readFrom(TradeSource.tradeSource())
     .withoutTimestamps()

    // Convert Trade stream to EnrichedTrade stream
    // - Trade (dto.Trade) has a symbol field
    // - Use LOOKUP_TABLE to look up full company name based on the symbol
    // - Create new Enriched Trade (dto.EnrichedTrade) using Trade and company name

    .writeTo(Sinks.logger());

    return p;
}
 
Example #28
Source File: MapTransactionContextConflictTest.java    From hazelcast-simulator with Apache License 2.0 5 votes vote down vote up
@Prepare(global = true)
public void prepare() {
    IMap<Integer, Long> map = targetInstance.getMap(name);
    for (int i = 0; i < keyCount; i++) {
        map.put(i, 0L);
    }
}
 
Example #29
Source File: MapTransactionContextConflictTest.java    From hazelcast-simulator with Apache License 2.0 5 votes vote down vote up
@Verify(global = false)
public void verify() {
    IList<TxnCounter> counts = targetInstance.getList(name + "count");
    TxnCounter total = new TxnCounter();
    for (TxnCounter c : counts) {
        total.add(c);
    }
    logger.info(name + ": " + total + " from " + counts.size() + " worker threads");

    IList<long[]> allIncrements = targetInstance.getList(name + "inc");
    long[] expected = new long[keyCount];
    for (long[] incs : allIncrements) {
        for (int i = 0; i < incs.length; i++) {
            expected[i] += incs[i];
        }
    }

    IMap<Integer, Long> map = targetInstance.getMap(name);
    int failures = 0;
    for (int i = 0; i < keyCount; i++) {
        if (expected[i] != map.get(i)) {
            failures++;
            logger.info(name + ": key=" + i + " expected " + expected[i] + " != " + "actual " + map.get(i));
        }
    }
    assertEquals(name + ": " + failures + " key=>values have been incremented unExpected", 0, failures);
}
 
Example #30
Source File: MapTransactionGetForUpdateTest.java    From hazelcast-simulator with Apache License 2.0 5 votes vote down vote up
@Prepare(global = true)
public void prepare() {
    IMap<Integer, Long> map = targetInstance.getMap(name);
    for (int i = 0; i < keyCount; i++) {
        map.put(i, 0L);
    }
}