Skip to content

Commit

Permalink
Extract StartupStatus class
Browse files Browse the repository at this point in the history
  • Loading branch information
electrum committed Dec 29, 2021
1 parent b99cab2 commit 1454ff5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
2 changes: 1 addition & 1 deletion core/trino-main/src/main/java/io/trino/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private void doStart(String trinoVersion)

injector.getInstance(Announcer.class).start();

injector.getInstance(ServerInfoResource.class).startupComplete();
injector.getInstance(StartupStatus.class).startupComplete();

log.info("======== SERVER STARTED ========");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@
import javax.ws.rs.core.Response;

import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;

import static com.google.common.base.Preconditions.checkState;
import static io.airlift.units.Duration.nanosSince;
import static io.trino.metadata.NodeState.ACTIVE;
import static io.trino.metadata.NodeState.SHUTTING_DOWN;
Expand All @@ -51,24 +49,25 @@ public class ServerInfoResource
private final String environment;
private final boolean coordinator;
private final GracefulShutdownHandler shutdownHandler;
private final StartupStatus startupStatus;
private final long startTime = System.nanoTime();
private final AtomicBoolean startupComplete = new AtomicBoolean();

@Inject
public ServerInfoResource(NodeVersion nodeVersion, NodeInfo nodeInfo, ServerConfig serverConfig, GracefulShutdownHandler shutdownHandler)
public ServerInfoResource(NodeVersion nodeVersion, NodeInfo nodeInfo, ServerConfig serverConfig, GracefulShutdownHandler shutdownHandler, StartupStatus startupStatus)
{
this.version = requireNonNull(nodeVersion, "nodeVersion is null");
this.environment = requireNonNull(nodeInfo, "nodeInfo is null").getEnvironment();
this.coordinator = requireNonNull(serverConfig, "serverConfig is null").isCoordinator();
this.shutdownHandler = requireNonNull(shutdownHandler, "shutdownHandler is null");
this.startupStatus = requireNonNull(startupStatus, "startupStatus is null");
}

@ResourceSecurity(PUBLIC)
@GET
@Produces(APPLICATION_JSON)
public ServerInfo getInfo()
{
boolean starting = !startupComplete.get();
boolean starting = !startupStatus.isStartupComplete();
return new ServerInfo(version, environment, coordinator, starting, Optional.of(nanosSince(startTime)));
}

Expand Down Expand Up @@ -126,9 +125,4 @@ public Response getServerCoordinator()
// return 404 to allow load balancers to only send traffic to the coordinator
return Response.status(Response.Status.NOT_FOUND).build();
}

public void startupComplete()
{
checkState(startupComplete.compareAndSet(false, true), "Server startup already marked as complete");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ protected void setup(Binder binder)
install(new WorkerModule());
}

binder.bind(StartupStatus.class).in(Scopes.SINGLETON);

configBinder(binder).bindConfigDefaults(HttpServerConfig.class, httpServerConfig -> {
httpServerConfig.setAdminEnabled(false);
});
Expand Down
33 changes: 33 additions & 0 deletions core/trino-main/src/main/java/io/trino/server/StartupStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.trino.server;

import java.util.concurrent.atomic.AtomicBoolean;

import static com.google.common.base.Preconditions.checkState;

public final class StartupStatus
{
private final AtomicBoolean startupComplete = new AtomicBoolean();

public void startupComplete()
{
checkState(startupComplete.compareAndSet(false, true), "Server startup already marked as complete");
}

public boolean isStartupComplete()
{
return startupComplete.get();
}
}

0 comments on commit 1454ff5

Please sign in to comment.