/*
* Copyright © 2014 Cask Data, Inc.
*
* 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 co.cask.cdap.gateway;
import co.cask.cdap.common.conf.CConfiguration;
import co.cask.cdap.common.conf.Constants;
import co.cask.cdap.common.hooks.MetricsReporterHook;
import co.cask.cdap.common.metrics.MetricsCollectionService;
import co.cask.http.HttpHandler;
import co.cask.http.NettyHttpService;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.AbstractIdleService;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import org.apache.twill.common.Cancellable;
import org.apache.twill.discovery.Discoverable;
import org.apache.twill.discovery.DiscoveryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
/**
* Gateway implemented using the common http netty framework.
*/
public class Gateway extends AbstractIdleService {
private static final Logger LOG = LoggerFactory.getLogger(Gateway.class);
private final NettyHttpService httpService;
private final DiscoveryService discoveryService;
private Cancellable cancelDiscovery;
@Inject
public Gateway(CConfiguration cConf,
@Named(Constants.Gateway.ADDRESS) InetAddress hostname,
Set<HttpHandler> handlers, DiscoveryService discoveryService,
@Nullable MetricsCollectionService metricsCollectionService) {
NettyHttpService.Builder builder = NettyHttpService.builder();
builder.addHttpHandlers(handlers);
builder.setHandlerHooks(ImmutableList.of(new MetricsReporterHook(metricsCollectionService,
Constants.Service.GATEWAY)));
builder.setHost(hostname.getCanonicalHostName());
builder.setPort(cConf.getInt(Constants.Gateway.PORT, Constants.Gateway.DEFAULT_PORT));
builder.setConnectionBacklog(cConf.getInt(Constants.Gateway.BACKLOG_CONNECTIONS,
Constants.Gateway.DEFAULT_BACKLOG));
builder.setExecThreadPoolSize(cConf.getInt(Constants.Gateway.EXEC_THREADS,
Constants.Gateway.DEFAULT_EXEC_THREADS));
builder.setBossThreadPoolSize(cConf.getInt(Constants.Gateway.BOSS_THREADS,
Constants.Gateway.DEFAULT_BOSS_THREADS));
builder.setWorkerThreadPoolSize(cConf.getInt(Constants.Gateway.WORKER_THREADS,
Constants.Gateway.DEFAULT_WORKER_THREADS));
this.httpService = builder.build();
this.discoveryService = discoveryService;
}
@Override
protected void startUp() throws Exception {
LOG.info("Starting Gateway...");
httpService.startAndWait();
// Register the service
cancelDiscovery = discoveryService.register(new Discoverable() {
@Override
public String getName() {
return Constants.Service.GATEWAY;
}
@Override
public InetSocketAddress getSocketAddress() {
return httpService.getBindAddress();
}
});
LOG.info("Gateway started successfully on {}", httpService.getBindAddress());
}
@Override
protected void shutDown() throws Exception {
LOG.info("Stopping Gateway...");
// Unregister the service
cancelDiscovery.cancel();
// Wait for a few seconds for requests to stop
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
LOG.error("Interrupted while waiting...", e);
}
httpService.stopAndWait();
}
public InetSocketAddress getBindAddress() {
return httpService.getBindAddress();
}
}