From 742db47e188286dd576a1637df90b1be2e4b6918 Mon Sep 17 00:00:00 2001 From: Tim Middleton Date: Fri, 6 Dec 2024 10:54:55 +0800 Subject: [PATCH 1/2] Ensure that federation menu is available on dashboard refresh --- .../demo/application/FederationResource.java | 8 ++ .../application/StartSecondaryResource.java | 2 + .../coherence/demo/application/Utilities.java | 86 +++++++++++-------- .../resources/cache-config-grid-edition.xml | 12 +++ src/main/resources/web/index.html | 2 +- .../resources/web/javascripts/controllers.js | 17 +++- 6 files changed, 89 insertions(+), 38 deletions(-) diff --git a/src/main/java/com/oracle/coherence/demo/application/FederationResource.java b/src/main/java/com/oracle/coherence/demo/application/FederationResource.java index 71ba787..ee43da1 100644 --- a/src/main/java/com/oracle/coherence/demo/application/FederationResource.java +++ b/src/main/java/com/oracle/coherence/demo/application/FederationResource.java @@ -153,6 +153,14 @@ public Response federationCommand(@PathParam("command") String command) { return Response.serverError().build(); } + @GET + @Path("/isStarted") + @Produces( {TEXT_PLAIN}) + public Response federationCommand() { + return Response.ok(Utilities.isFederationStarted()).build(); + } + + /** * Obtain the name of the FederationMBean for a given service. * diff --git a/src/main/java/com/oracle/coherence/demo/application/StartSecondaryResource.java b/src/main/java/com/oracle/coherence/demo/application/StartSecondaryResource.java index 33aff89..f9c54e2 100644 --- a/src/main/java/com/oracle/coherence/demo/application/StartSecondaryResource.java +++ b/src/main/java/com/oracle/coherence/demo/application/StartSecondaryResource.java @@ -141,6 +141,8 @@ public Response startCluster() { registry.registerResource(CoherenceCacheServer.class, "secondary", server); + Utilities.setFederationStarted(); + // return the member-id return Response.ok("secondary").build(); } diff --git a/src/main/java/com/oracle/coherence/demo/application/Utilities.java b/src/main/java/com/oracle/coherence/demo/application/Utilities.java index b61a854..efca0e4 100644 --- a/src/main/java/com/oracle/coherence/demo/application/Utilities.java +++ b/src/main/java/com/oracle/coherence/demo/application/Utilities.java @@ -99,14 +99,19 @@ public final class Utilities public static final String PRICE_CACHE = "Price"; + /** + * The name of the federation status cache. + */ + public static final String FEDERATION_STATUS = "federation-status"; + + // ----- constructors --------------------------------------------------- /** * Instances not allowed. */ - private Utilities() - { + private Utilities() { throw new IllegalStateException("illegal instantiation"); } @@ -119,8 +124,7 @@ private Utilities() * * @param args arguments to main */ - public static void main(String[] args) - { + public static void main(String[] args) { createPositions(null, NR_POSITIONS_TO_CREATE); } @@ -130,8 +134,7 @@ public static void main(String[] args) * * @return the trade {@link NamedCache} */ - public static NamedCache getTradesCache() - { + public static NamedCache getTradesCache() { return Coherence.getInstance().getSession().getCache(TRADE_CACHE); } @@ -141,12 +144,21 @@ public static NamedCache getTradesCache() * * @return the price {@link NamedCache} */ - public static NamedCache getPricesCache() - { + public static NamedCache getPricesCache() { return Coherence.getInstance().getSession().getCache(PRICE_CACHE); } + /** + * Obtain the federation-status cache. + * + * @return the price {@link NamedCache} + */ + public static NamedCache getFederationStatusCache() { + return Coherence.getInstance().getSession().getCache(FEDERATION_STATUS); + } + + /** * Obtain an indicator showing if we are running under the Coherence Operator in * Kubernetes. @@ -154,8 +166,7 @@ public static NamedCache getPricesCache() * @return an indicator showing if we are running under the Coherence Operator in * Kubernetes */ - public static boolean isRunningInKubernetes() - { + public static boolean isRunningInKubernetes() { return System.getenv("KUBERNETES_SERVICE_HOST") != null && System.getenv("KUBERNETES_SERVICE_PORT") != null; } @@ -165,8 +176,7 @@ public static boolean isRunningInKubernetes() * * @return an indicator showing if we have enabled metrics */ - public static boolean isMetricsEnabled() - { + public static boolean isMetricsEnabled() { Enumeration serviceNames = CacheFactory.ensureCluster().getServiceNames(); while (serviceNames.hasMoreElements()) { if ("MetricsHttpProxy".equals(serviceNames.nextElement())) { @@ -176,6 +186,22 @@ public static boolean isMetricsEnabled() return false; } + /** + * Set federation to be started. + */ + public static void setFederationStarted() + { + getFederationStatusCache().put("status", true); + } + + /** + * Indicates if federation has been started. + * + * @return true if federation has been started + */ + public static boolean isFederationStarted() { + return getFederationStatusCache().getOrDefault("status", false); + } /** @@ -183,8 +209,7 @@ public static boolean isMetricsEnabled() * * @return the Coherence cluster version */ - public static String getCoherenceVersion() - { + public static String getCoherenceVersion() { return CacheFactory.VERSION.replaceFirst(" .*$", "") .replaceFirst("[.-]SNAPSHOT.*$", "") .replaceAll("-", "."); @@ -196,8 +221,7 @@ public static String getCoherenceVersion() * * @return an indicator showing if federation is configured in K8s. */ - public static boolean isFederationConfiguredInK8s() - { + public static boolean isFederationConfiguredInK8s() { return isRunningInKubernetes() && System.getProperty("primary.cluster") != null && System.getProperty("secondary.cluster") != null && @@ -220,8 +244,7 @@ public static int getCoherenceVersionAsInt() /** * Add indexes to the caches to improve query performance. */ - public static void addIndexes() - { + public static void addIndexes() { NamedCache tradesCache = getTradesCache(); Tracer tracer = GlobalTracer.get(); Span span = tracer.buildSpan("Utilities.AddIndexes") @@ -229,8 +252,7 @@ public static void addIndexes() .withTag(Tags.SPAN_KIND, Tags.SPAN_KIND_SERVER).start(); Logger.out("Adding Indexes..."); - try (Scope ignored = tracer.activateSpan(span)) - { + try (Scope ignored = tracer.activateSpan(span)) { tradesCache.addIndex(Trade::getSymbol, true, null); spanLog(span, "Created trade symbol index"); tradesCache.addIndex(Trade::getPurchaseValue, false, null); @@ -238,8 +260,7 @@ public static void addIndexes() tradesCache.addIndex(Trade::getQuantity, false, null); spanLog(span, "Created trade amount index"); } - finally - { + finally { span.finish(); } Logger.out(" Done"); @@ -249,8 +270,7 @@ public static void addIndexes() /** * Remove indexes to the caches. */ - public static void removeIndexes() - { + public static void removeIndexes() { NamedCache tradesCache = getTradesCache(); Tracer tracer = GlobalTracer.get(); Span span = tracer.buildSpan("Utilities.RemoveIndexes") @@ -258,8 +278,7 @@ public static void removeIndexes() .withTag(Tags.SPAN_KIND, Tags.SPAN_KIND_SERVER).start(); Logger.out("Removing Indexes..."); - try (Scope ignored = tracer.activateSpan(span)) - { + try (Scope ignored = tracer.activateSpan(span)) { tradesCache.removeIndex(Trade::getSymbol); spanLog(span, "Removed trade symbol index"); tradesCache.removeIndex(Trade::getPurchaseValue); @@ -267,8 +286,7 @@ public static void removeIndexes() tradesCache.removeIndex(Trade::getQuantity); spanLog(span, "Removed trade amount index"); } - finally - { + finally { span.finish(); } @@ -280,8 +298,7 @@ public static void removeIndexes() * Populate initial prices for symbols. Make the current price for all * symbols to be $40 to make it fair and un-biased. */ - public static void populatePrices() - { + public static void populatePrices() { NamedCache pricesCaches = getPricesCache(); Tracer tracer = GlobalTracer.get(); Span span = tracer.buildSpan("Utilities.PopulatePrices") @@ -289,16 +306,13 @@ public static void populatePrices() .withTag(Tags.SPAN_KIND, Tags.SPAN_KIND_SERVER) .withTag("symbol.count", SYMBOLS.length).start(); - try (Scope ignored = tracer.activateSpan(span)) - { - for (String symbol : SYMBOLS) - { + try (Scope ignored = tracer.activateSpan(span)) { + for (String symbol : SYMBOLS) { Price price = new Price(symbol, INITIAL_PRICE); pricesCaches.put(price.getSymbol(), price); } } - finally - { + finally { span.finish(); } } diff --git a/src/main/resources/cache-config-grid-edition.xml b/src/main/resources/cache-config-grid-edition.xml index b8ba2cc..5a463b4 100644 --- a/src/main/resources/cache-config-grid-edition.xml +++ b/src/main/resources/cache-config-grid-edition.xml @@ -42,10 +42,18 @@ Trade federated-scheme + java.lang.String + com.oracle.coherence.demo.model.Trade Price federated-scheme + java.lang.String + com.oracle.coherence.demo.model.Price + + + federation-status + federation-status-scheme @@ -105,6 +113,10 @@ + + federation-status-scheme + + InvocationService true diff --git a/src/main/resources/web/index.html b/src/main/resources/web/index.html index aaa3f49..59defdc 100644 --- a/src/main/resources/web/index.html +++ b/src/main/resources/web/index.html @@ -80,7 +80,7 @@