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

Enable service discovery in controller too #8193

Merged
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 @@ -26,6 +26,7 @@
import org.apache.pinot.broker.requesthandler.BrokerRequestHandler;
import org.apache.pinot.broker.routing.RoutingManager;
import org.apache.pinot.common.metrics.BrokerMetrics;
import org.apache.pinot.core.api.ServiceAutoDiscoveryFeature;
import org.apache.pinot.core.transport.ListenerConfig;
import org.apache.pinot.core.util.ListenerConfigUtil;
import org.apache.pinot.spi.env.PinotConfiguration;
Expand All @@ -51,7 +52,7 @@ public BrokerAdminApiApplication(RoutingManager routingManager, BrokerRequestHan
packages(RESOURCE_PACKAGE);
property(PINOT_CONFIGURATION, brokerConf);
if (brokerConf.getProperty(CommonConstants.Broker.BROKER_SERVICE_AUTO_DISCOVERY, false)) {
register(BrokerServiceAutoDiscoveryFeature.class);
register(ServiceAutoDiscoveryFeature.class);
}
register(new AbstractBinder() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import javax.ws.rs.container.ContainerResponseFilter;
import org.apache.pinot.controller.ControllerConf;
import org.apache.pinot.controller.api.access.AuthenticationFilter;
import org.apache.pinot.core.api.ServiceAutoDiscoveryFeature;
import org.apache.pinot.core.transport.ListenerConfig;
import org.apache.pinot.core.util.ListenerConfigUtil;
import org.apache.pinot.spi.utils.CommonConstants;
Expand Down Expand Up @@ -57,6 +58,9 @@ public ControllerAdminApiApplication(ControllerConf conf) {
packages(_controllerResourcePackages);
// TODO See ControllerResponseFilter
// register(new LoggingFeature());
if (conf.getProperty(CommonConstants.Controller.CONTROLLER_SERVICE_AUTO_DISCOVERY, false)) {
register(ServiceAutoDiscoveryFeature.class);
}
register(JacksonFeature.class);
register(MultiPartFeature.class);
registerClasses(io.swagger.jaxrs.listing.ApiListingResource.class);
Expand Down
4 changes: 4 additions & 0 deletions pinot-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@
<groupId>org.glassfish.grizzly</groupId>
<artifactId>grizzly-http-server</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2-locator</artifactId>
</dependency>

<!-- test -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.pinot.broker.broker;
package org.apache.pinot.core.api;

import java.io.IOException;
import javax.inject.Inject;
Expand All @@ -31,6 +31,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
* Auto scan and discovery classes annotated with Service tags. This enables the feature similar to Spring's
* auto-discovery, that if a class is annotated with some tags like @Service, the class will be auto-loaded
Expand Down Expand Up @@ -68,8 +69,8 @@
* pinot-integration-tests/src/main/java/org/apache/pinot/broker/integration/tests/BrokerTestAutoLoadedService.java
* </code>
*/
public class BrokerServiceAutoDiscoveryFeature implements Feature {
private static final Logger LOGGER = LoggerFactory.getLogger(BrokerServiceAutoDiscoveryFeature.class);
public class ServiceAutoDiscoveryFeature implements Feature {
Copy link
Contributor

Choose a reason for hiding this comment

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

What will happen if we have PinotServiceManager deploying controller/broker/server in the same JVM and all of them trying to load this ServiceAutoDiscoveryFeature?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A very good point. I added some code (static boolean) to make sure discovery is done once per JVM, and did the integration test that starts Broker+Controller in the same test.
The result shows that only one of the Broker/Controller can find service properly.
It seems that based on Jersey, Services/Features are isolated per Application (tied to ResourceConfig) level, so if there are two applications, the discovery has to be done twice.

private static final Logger LOGGER = LoggerFactory.getLogger(ServiceAutoDiscoveryFeature.class);

@Inject
ServiceLocator _serviceLocator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.apache.pinot.broker.integration.tests.BrokerTestAutoLoadedService;
import org.apache.pinot.core.api.AutoLoadedServiceForTest;

/**
* This class is a typical "echo" service that will return whatever string you call GET with a path.
Expand All @@ -35,9 +35,9 @@
*/
@Api(tags = "Test")
@Path("/test")
public class EchoWithAutoDiscovery {
public class BrokerEchoWithAutoDiscovery {
@Inject
public BrokerTestAutoLoadedService _injectedService;
public AutoLoadedServiceForTest _injectedService;
@GET
@Path("/echo/{table}")
@Produces(MediaType.TEXT_PLAIN)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.pinot.controller.api.resources;

import io.swagger.annotations.Api;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.apache.pinot.core.api.AutoLoadedServiceForTest;


/**
* This class is a typical "echo" service that will return whatever string you call GET with a path.
* It is both an integration test and a demonstration of how to dynamically add an endpoint to broker,
* create auto-service discovery
*/
@Api(tags = "Test")
@Path("/test")
public class ControllerEchoWithAutoDiscovery {
@Inject
public AutoLoadedServiceForTest _injectedService;
@GET
@Path("/echo/{table}")
@Produces(MediaType.TEXT_PLAIN)
public String echo(@PathParam("table") String table) {
return _injectedService.echo(table);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.pinot.broker.integration.tests;
package org.apache.pinot.core.api;

import javax.inject.Singleton;
import org.jvnet.hk2.annotations.Service;

@Service
@Singleton
public class BrokerTestAutoLoadedService {
public class AutoLoadedServiceForTest {
public String echo(String echoText) {
return echoText;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.testng.annotations.Test;

/**
* Integration test that converts Avro data for 12 segments and runs queries against it.
* Integration test that starts one broker with auto-discovered echo service and test it
*/
public class BrokerServiceDiscoveryIntegrationTest extends BaseClusterIntegrationTestSet {
private static final String TENANT_NAME = "TestTenant";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.pinot.integration.tests;

import java.util.Map;
import org.apache.commons.io.FileUtils;
import org.apache.pinot.spi.env.PinotConfiguration;
import org.apache.pinot.spi.utils.CommonConstants;
import org.apache.pinot.util.TestUtils;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;


/**
* Integration test that starts one broker with auto-discovered echo service and test it
*/
public class ControllerServiceDiscoveryIntegrationTest extends BaseClusterIntegrationTestSet {
private static final String TENANT_NAME = "TestTenant";

@Override
protected String getBrokerTenant() {
return TENANT_NAME;
}

@Override
protected String getServerTenant() {
return TENANT_NAME;
}

@Override
public Map<String, Object> getDefaultControllerConfiguration() {
Map<String, Object> retVal = super.getDefaultControllerConfiguration();
retVal.put(CommonConstants.Controller.CONTROLLER_SERVICE_AUTO_DISCOVERY, true);
return retVal;
}

@Override
protected PinotConfiguration getDefaultBrokerConfiguration() {
PinotConfiguration config = new PinotConfiguration();
config.setProperty(CommonConstants.Broker.BROKER_SERVICE_AUTO_DISCOVERY, true);
return config;
}

@BeforeClass
public void setUp()
throws Exception {
TestUtils.ensureDirectoriesExistAndEmpty(_tempDir, _segmentDir, _tarDir);

// Start the Pinot cluster
startZk();
startController();
startBrokers(1);
startServers(1);
}
Copy link
Member

Choose a reason for hiding this comment

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

I’m surprised the linter passed, there should be a blank line here. We will need to check if checkstyle can catch this.

Copy link
Contributor

Choose a reason for hiding this comment

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

#8197

You triggered my OCD...

@AfterClass
public void tearDown()
throws Exception {

// Brokers and servers has been stopped
stopBroker();
stopController();
stopZk();
FileUtils.deleteDirectory(_tempDir);
}

@Test
public void testControllerExtraEndpointsAutoLoaded()
throws Exception {
String response = sendGetRequest(_controllerBaseApiUrl + "/test/echo/doge");
Assert.assertEquals(response, "doge");
response = sendGetRequest(_brokerBaseApiUrl + "/test/echo/doge");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@xiangfu0 the test here will fail if we do initialization only once

Assert.assertEquals(response, "doge");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,8 @@ public static class Controller {
public static final String CONFIG_OF_INSTANCE_ID = "pinot.controller.instance.id";
public static final String CONFIG_OF_CONTROLLER_QUERY_REWRITER_CLASS_NAMES =
"pinot.controller.query.rewriter.class.names";
//Set to true to load all services tagged and compiled with hk2-metadata-generator. Default to False
public static final String CONTROLLER_SERVICE_AUTO_DISCOVERY = "pinot.controller.service.auto.discovery";
}

public static class Minion {
Expand Down