package org.folio.okapi.util;

import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.shareddata.AsyncMap;
import io.vertx.core.shareddata.SharedData;
import java.util.Random;
import org.folio.okapi.common.ExtendedAsyncResult;
import org.folio.okapi.common.Failure;
import org.folio.okapi.common.Success;
import static org.folio.okapi.common.ErrorType.*;

/**
 * Factory to create either a vert.x ClusterWideMap or a
 * AsyncLocalmap, if not running in a clustered mode.
 */
class AsyncMapFactory {

  private AsyncMapFactory() {
    throw new IllegalAccessError("AsyncMapFactory");
  }

  /**
   * Create a AsyncMap
   *
   * @param <K> Key type
   * @param <V> Value type
   * @param vertx
   * @param mapName name of the map. If null, will always create a local map
   * @param fut
   */
  public static <K, V> void create(Vertx vertx, String mapName,          Handler<ExtendedAsyncResult<AsyncMap<K, V>>> fut) {
    if (vertx.isClustered() && mapName != null) {
      SharedData shared = vertx.sharedData();
      shared.<K, V>getClusterWideMap(mapName, res -> {
        if (res.succeeded()) {
          fut.handle(new Success<>(res.result()));
        } else {
          fut.handle(new Failure<>(INTERNAL, res.cause()));
        }
      });
    } else {
      // Dirty trickery to make sure we can run two verticles in our tests,
      // without them sharing the 'shared' memory. Only when running in non-
      // clustered mode, of course.
      // Also used in deploy-only nodes, where we want local-only tenant and
      // module lists with only the hard-coded supertenant and internalModule.
      Random r = new Random();
      String newid = String.format("%09d", r.nextInt(1000000000));
      if (mapName != null) {
        newid = mapName + newid;
      }
      AsyncLocalmap<K, V> l = new AsyncLocalmap<>(vertx, newid);
      fut.handle(new Success<>(l));
    }
  }
}

@@ -172,33 +172,8 @@
 ta;%0A
-import java.util.Random;%0A
 impo
@@ -219,16 +219,16 @@
 Result;%0A
+
 import o
@@ -408,19 +408,16 @@
 Map or a
-%0A *
  AsyncLo
@@ -430,16 +430,19 @@
 , if not
+%0A *
  running
@@ -846,22 +846,17 @@
 mapName,
-      
+%0A
     Hand
@@ -899,24 +899,25 @@
 V%3E%3E%3E fut) %7B%0A
+%0A
     if (vert
@@ -1602,97 +1602,63 @@
     
-Random r = new Random();%0A      String newid = String.format(%22%2509d%22, r.nextInt(1000000000)
+String newid = vertx.getOrCreateContext().deploymentID(
 );%0A 

Use VerticleID rather than ramdom for local map