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

Added Rotation Management #63

Merged
merged 13 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
@@ -0,0 +1,24 @@
package com.flipkart.gjex.core.config;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not see any usage of this ResourceConfig. Can we please remove it ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed


import com.flipkart.gjex.core.web.RotationManagementResource;
import org.glassfish.jersey.server.ResourceConfig;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

/**
* RotationManagementConfig for registering RotationManagementResource.
* @author ajay.jalgaonkar
*
*/

@Singleton
@Named("RotationManagementConfig")
public class RotationManagementConfig extends ResourceConfig {

@Inject
public RotationManagementConfig (RotationManagementResource rotationManagementResource) {
register(rotationManagementResource);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.flipkart.gjex.core.task;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RotationManagementBasedHealthCheck does not belong to core/task package . Can you please create a healthcheck package under code and move healthcheckRegistry.java as well to it .

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved


import com.flipkart.gjex.core.logging.Logging;
import io.dropwizard.metrics5.health.HealthCheck;

import javax.inject.Singleton;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* Rotation management based health check for the app
* @author ajay.jalgaonkar
*/

@Singleton
public class RotationManagementBasedHealthCheck extends HealthCheck implements Logging {
private static final String BIR = "bir";
private static final String OOR = "oor";

private AtomicBoolean state;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we refactor state attribute to maybe inRotation to signify what it will actually store .

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactored to rotationStatus

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make this AtomicBoolean a final as well . We do not require more than 1 instance of this attribute

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


public RotationManagementBasedHealthCheck() {
state = new AtomicBoolean(true);
}

@Override
protected Result check() {
if (isBir()) {
info("Returning healthy status.");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel these info logs are not necessary for every health check request.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

return Result.healthy("Server is " + getStatus());
} else {
info("Returning unhealthy status.");
return Result.unhealthy("Server is " + getStatus());
}
}

public String getStatus() {
return isBir() ? BIR : OOR;
}

public String makeOor() {
state.set(false);
return OOR;
}

public String makeBir() {
state.set(true);
return BIR;
}

public boolean isBir() {
return state.get();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.flipkart.gjex.core.web;

import com.flipkart.gjex.core.task.RotationManagementBasedHealthCheck;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import javax.servlet.ServletContext;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

/**
* Resource for rotation management
* @author ajay.jalgaonkar
*/

@Singleton
@Path("/")
@Named
public class RotationManagementResource {

@Context
private ServletContext servletContext;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not used . Can we please remove it

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed


@Inject
public RotationManagementResource(RotationManagementBasedHealthCheck rotationManagementBasedHealthCheck) {
this.rotationManagementBasedHealthCheck = rotationManagementBasedHealthCheck;
}

private RotationManagementBasedHealthCheck rotationManagementBasedHealthCheck;

@GET
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ajay-jalgaonkar Lets not have this as GET's. A modification status should be an request which cannot be triggered by mistake or by automatic refresh. Lets move this to POST. Same with bir.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@Path("/oor")
@Produces(MediaType.APPLICATION_JSON)
public Response oor() {
String response = this.rotationManagementBasedHealthCheck.makeOor();
return Response.status(Response.Status.OK).entity(response).build();
}

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/bir")
public Response bir() {
String response = this.rotationManagementBasedHealthCheck.makeBir();
return Response.status(Response.Status.OK).entity(response).build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,22 @@
*/
package com.flipkart.gjex.examples.helloworld.guice;

import org.glassfish.jersey.server.ResourceConfig;

import io.dropwizard.metrics5.health.HealthCheck;
import com.flipkart.gjex.core.filter.Filter;
import com.flipkart.gjex.core.task.RotationManagementBasedHealthCheck;
import com.flipkart.gjex.core.tracing.TracingSampler;
import com.flipkart.gjex.examples.helloworld.filter.AuthFilter;
import com.flipkart.gjex.examples.helloworld.filter.LoggingFilter;
import com.flipkart.gjex.examples.helloworld.healthcheck.AllIsWellHealthCheck;
import com.flipkart.gjex.examples.helloworld.service.GreeterService;
import com.flipkart.gjex.examples.helloworld.tracing.AllWhitelistTracingSampler;
import com.flipkart.gjex.examples.helloworld.web.HelloWorldResourceConfig;
import com.google.inject.AbstractModule;
import com.google.inject.name.Names;

import io.dropwizard.metrics5.health.HealthCheck;
import io.grpc.BindableService;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.examples.helloworld.GreeterGrpc;


import org.glassfish.jersey.server.ResourceConfig;

/**
* Guice module for wiring sample Service to GJEX runtime
Expand All @@ -56,7 +52,7 @@ protected void configure() {
bind(Filter.class).annotatedWith(Names.named("LoggingFilter")).to(LoggingFilter.class);
bind(Filter.class).annotatedWith(Names.named("AuthFilter")).to(AuthFilter.class);
bind(TracingSampler.class).to(AllWhitelistTracingSampler.class);
bind(HealthCheck.class).to(AllIsWellHealthCheck.class);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kingster Should we remove AllIsWellHealthCheck.java altogether considering we are providing a health check implementation by default as part of server?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can, but then users shouldn't need to bind this. This should be part of the defaults then.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed AllIsWellHealthCheck

Copy link
Member

@kingster kingster Apr 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be by default now. Lets change this

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now binding of RotationManagementBasedHealthCheck is done in ApiModule

bind(HealthCheck.class).to(RotationManagementBasedHealthCheck.class);
bind(ResourceConfig.class).annotatedWith(Names.named("HelloWorldResourceConfig")).to(HelloWorldResourceConfig.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
*/
package com.flipkart.gjex.examples.helloworld.web;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert this file

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reverted


import com.flipkart.gjex.core.config.RotationManagementConfig;
import com.flipkart.gjex.core.web.RotationManagementResource;
import org.glassfish.jersey.server.ResourceConfig;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import org.glassfish.jersey.server.ResourceConfig;

/**
* ResourceConfig example for registering all custom web resources for a GJEX application.
* @author regu.b
Expand All @@ -31,8 +33,8 @@
public class HelloWorldResourceConfig extends ResourceConfig {

@Inject
public HelloWorldResourceConfig (HelloWorldResource1 helloWorldresource1,
HelloWorldResource2 helloWorldresource2) {
public HelloWorldResourceConfig (HelloWorldResource1 helloWorldresource1,
HelloWorldResource2 helloWorldresource2) {
register(helloWorldresource1);
register(helloWorldresource2);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import javax.inject.Named;
import javax.inject.Singleton;

import com.flipkart.gjex.core.web.RotationManagementResource;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.DefaultServlet;
Expand Down Expand Up @@ -139,13 +140,21 @@ Server getDashboardJettyServer(@Named("Dashboard.service.port") int port,
@Singleton
Server getAPIJettyServer(@Named("APIVanillaJettyServer")Server server,
@Named("ApiServletContext") ServletContextHandler context,
@Named("HealthCheckResourceConfig")ResourceConfig healthcheckResourceConfig,
@Named("HealthCheckResourceConfig") ResourceConfig healthCheckResourceConfig,
@Named("RotationManagementResourceConfig") ResourceConfig rotationManagementResourceConfig,
@Named("TracingResourceConfig")ResourceConfig tracingResourceConfig,
@Named("TracingSamplerHolder")TracingSamplerHolder tracingSamplerHolder,
@Named("JSONMarshallingProvider")JacksonJaxbJsonProvider provider) throws URISyntaxException, UnknownHostException {
healthcheckResourceConfig.register(provider);
ServletHolder healthcheckServlet = new ServletHolder(new ServletContainer(healthcheckResourceConfig));
context.addServlet(healthcheckServlet, "/healthcheck"); // registering Health Check servlet under the /healthcheck path
healthCheckResourceConfig.register(provider);
ServletHolder healthCheckServlet =
new ServletHolder(new ServletContainer(healthCheckResourceConfig));
context.addServlet(healthCheckServlet, "/healthcheck"); // registering Health Check servlet under the /healthcheck path

rotationManagementResourceConfig.register(provider);
ServletHolder rotationManagementServlet =
new ServletHolder(new ServletContainer(rotationManagementResourceConfig));
context.addServlet(rotationManagementServlet, "/rotation/*"); // registering Rotation
// Management servlet under the /rotation path

tracingResourceConfig.register(provider);
ServletHolder tracingServlet = new ServletHolder(new ServletContainer(tracingResourceConfig));
Expand Down Expand Up @@ -209,6 +218,16 @@ ResourceConfig getAPIResourceConfig(HealthCheckResource healthCheckResource) {
return resourceConfig;
}

@Named("RotationManagementResourceConfig")
@Singleton
@Provides
ResourceConfig getRotationManagementResourceConfig(RotationManagementResource rotationManagementResource) {
ResourceConfig resourceConfig = new ResourceConfig();
resourceConfig.register(rotationManagementResource);
resourceConfig.setApplicationName(Constants.GJEX_CORE_APPLICATION);
return resourceConfig;
}

@Named("TracingResourceConfig")
@Singleton
@Provides
Expand Down