Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that federation menu is available on dashboard refresh #159

Merged
merged 2 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ public Response startCluster() {

registry.registerResource(CoherenceCacheServer.class, "secondary", server);

Utilities.setFederationStarted();

// return the member-id
return Response.ok("secondary").build();
}
Expand Down
86 changes: 50 additions & 36 deletions src/main/java/com/oracle/coherence/demo/application/Utilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand All @@ -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);
}

Expand All @@ -130,8 +134,7 @@ public static void main(String[] args)
*
* @return the trade {@link NamedCache}
*/
public static NamedCache<String, Trade> getTradesCache()
{
public static NamedCache<String, Trade> getTradesCache() {
return Coherence.getInstance().getSession().getCache(TRADE_CACHE);
}

Expand All @@ -141,21 +144,29 @@ public static NamedCache<String, Trade> getTradesCache()
*
* @return the price {@link NamedCache}
*/
public static NamedCache<String, Price> getPricesCache()
{
public static NamedCache<String, Price> getPricesCache() {
return Coherence.getInstance().getSession().getCache(PRICE_CACHE);
}


/**
* Obtain the federation-status cache.
*
* @return the price {@link NamedCache}
*/
public static NamedCache<String, Boolean> getFederationStatusCache() {
return Coherence.getInstance().getSession().getCache(FEDERATION_STATUS);
}


/**
* Obtain an indicator showing if we are running under the Coherence Operator in
* Kubernetes.
*
* @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;
}
Expand All @@ -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<String> serviceNames = CacheFactory.ensureCluster().getServiceNames();
while (serviceNames.hasMoreElements()) {
if ("MetricsHttpProxy".equals(serviceNames.nextElement())) {
Expand All @@ -176,15 +186,30 @@ 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);
}


/**
* Obtain the Coherence cluster version.
*
* @return the Coherence cluster version
*/
public static String getCoherenceVersion()
{
public static String getCoherenceVersion() {
return CacheFactory.VERSION.replaceFirst(" .*$", "")
.replaceFirst("[.-]SNAPSHOT.*$", "")
.replaceAll("-", ".");
Expand All @@ -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 &&
Expand All @@ -220,26 +244,23 @@ public static int getCoherenceVersionAsInt()
/**
* Add indexes to the caches to improve query performance.
*/
public static void addIndexes()
{
public static void addIndexes() {
NamedCache<String, Trade> tradesCache = getTradesCache();
Tracer tracer = GlobalTracer.get();
Span span = tracer.buildSpan("Utilities.AddIndexes")
.withTag(Tags.COMPONENT, "demo")
.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);
spanLog(span, "Created trade purchase value index");
tradesCache.addIndex(Trade::getQuantity, false, null);
spanLog(span, "Created trade amount index");
}
finally
{
finally {
span.finish();
}
Logger.out(" Done");
Expand All @@ -249,26 +270,23 @@ public static void addIndexes()
/**
* Remove indexes to the caches.
*/
public static void removeIndexes()
{
public static void removeIndexes() {
NamedCache<String, Trade> tradesCache = getTradesCache();
Tracer tracer = GlobalTracer.get();
Span span = tracer.buildSpan("Utilities.RemoveIndexes")
.withTag(Tags.COMPONENT, "demo")
.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);
spanLog(span, "Removed trade purchase value index");
tradesCache.removeIndex(Trade::getQuantity);
spanLog(span, "Removed trade amount index");
}
finally
{
finally {
span.finish();
}

Expand All @@ -280,25 +298,21 @@ 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<String, Price> pricesCaches = getPricesCache();
Tracer tracer = GlobalTracer.get();
Span span = tracer.buildSpan("Utilities.PopulatePrices")
.withTag(Tags.COMPONENT, "demo")
.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();
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/main/resources/cache-config-grid-edition.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,18 @@
<cache-mapping>
<cache-name>Trade</cache-name>
<scheme-name>federated-scheme</scheme-name>
<key-type>java.lang.String</key-type>
<value-type>com.oracle.coherence.demo.model.Trade</value-type>
</cache-mapping>
<cache-mapping>
<cache-name>Price</cache-name>
<scheme-name>federated-scheme</scheme-name>
<key-type>java.lang.String</key-type>
<value-type>com.oracle.coherence.demo.model.Price</value-type>
</cache-mapping>
<cache-mapping>
<cache-name>federation-status</cache-name>
<scheme-name>federation-status-scheme</scheme-name>
</cache-mapping>
</caching-scheme-mapping>

Expand Down Expand Up @@ -105,6 +113,10 @@
</topologies>
</federated-scheme>

<local-scheme>
<scheme-name>federation-status-scheme</scheme-name>
</local-scheme>

<invocation-scheme>
<service-name>InvocationService</service-name>
<autostart>true</autostart>
Expand Down
3 changes: 1 addition & 2 deletions src/main/resources/web/fragments/clear.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
Obtain a strongly-typed NamedCache reference:<p>
<pre>
NamedCache&lt;UUID, Trade&gt; tradesCache = CacheFactory.getTypedCache("trades",
TypeAssertion.withTypes(UUID.class, Trade.class));
NamedCache&lt;UUID, Trade&gt; tradesCache = CacheFactory.getTypedCache("trades");
</pre>
<p>To clear the cache, issue the following:<p>
<pre>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
<ul class="nav navbar-top-links navbar-right" ng-cloak>

<li class="dropdown" >
<a href="#"><span id="statusMessage" ng-show="displayStatus" ng-class="{{lastStatusClass}}">{{lastStatusMessage}}</span>&nbsp;</a>
<strong><a href="#"><span id="statusMessage" ng-show="displayStatus" ng-class="{{lastStatusClass}}">{{lastStatusMessage}}</span>&nbsp;</a></strong>
</li>
<!-- /.dropdown -->
<li class="dropdown" ng-show="(!isRunningInKubernetes && clusterName != secondaryClusterName) || (isRunningInKubernetes && federationConfiguredInK8s && clusterName != secondaryClusterName)">
Expand Down
17 changes: 16 additions & 1 deletion src/main/resources/web/javascripts/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,10 @@ demoApp.controller('DemoController', ['$scope', '$http', '$interval', '$location
if (self.clusterName !== self.secondaryClusterName && !insightCookies.skipSplash) {
self.displayInsight('welcome');
}
});

// check if federation has already been started
self.checkFederation()
});
});

self.symbolsChartData = [];
Expand Down Expand Up @@ -837,6 +839,19 @@ demoApp.controller('DemoController', ['$scope', '$http', '$interval', '$location
$http.get('/service/federation/' + operation);
};

// ---- the function to check if federation has already been started

self.checkFederation = function() {
$http.get('/service/federation/isStarted').then(function(response) {
if (response.data === "true") {
self.secondaryCluster = 'enabled';
self.localClusterName = self.primaryClusterName;
self.federationControlLabel = self.STOP_FEDERATION;
}
});
};


// ---- the function to carry out persistence operations ----

self.persistenceOperation = function(operation) {
Expand Down
Loading