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
Add SelfDiscoveryResource; rename org.apache.druid.discovery.NodeType to NodeRole #6702
Changes from 1 commit
d1b12d7
b295155
2239684
da617e1
7cb1048
679a303
d27d0c4
72da228
abec2ad
7f07dca
4eed8d8
bd01a6b
766288e
f0a3c55
21792b1
8d02912
e019b1c
c4cb77b
e4a5bb9
6f07231
df813e1
81e36f0
3037e47
3926669
3737cb5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* 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.druid.server.http; | ||
|
||
import com.google.inject.Inject; | ||
import com.google.inject.Singleton; | ||
import com.sun.jersey.spi.container.ResourceFilters; | ||
import org.apache.druid.discovery.DiscoveryDruidNode; | ||
import org.apache.druid.discovery.DruidNodeDiscovery; | ||
import org.apache.druid.discovery.DruidNodeDiscoveryProvider; | ||
import org.apache.druid.discovery.NodeType; | ||
import org.apache.druid.guice.annotations.Self; | ||
import org.apache.druid.java.util.common.lifecycle.Lifecycle; | ||
import org.apache.druid.server.DruidNode; | ||
import org.apache.druid.server.http.security.StateResourceFilter; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
/** | ||
* This class is annotated {@link Singleton} rather than {@link org.apache.druid.guice.LazySingleton}, because it adds | ||
* a lifecycle handler in the constructor, that should happen before the lifecycle is started, i. e. eagerly during the | ||
* DI configuration phase. | ||
*/ | ||
@Singleton | ||
@Path("/selfDiscovered") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is a kind of status/health check, could consider putting the endpoint under There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renamed to |
||
@ResourceFilters(StateResourceFilter.class) | ||
public class SelfDiscoveryResource | ||
{ | ||
private boolean selfDiscovered = false; | ||
|
||
@Inject | ||
public SelfDiscoveryResource( | ||
@Self DruidNode thisDruidNode, | ||
@Self NodeType thisNodeType, | ||
DruidNodeDiscoveryProvider nodeDiscoveryProvider, | ||
Lifecycle lifecycle | ||
) | ||
{ | ||
Lifecycle.Handler selfDiscoveryListenerRegistrator = new Lifecycle.Handler() | ||
{ | ||
@Override | ||
public void start() | ||
{ | ||
registerSelfDiscoveryListener(thisDruidNode, thisNodeType, nodeDiscoveryProvider); | ||
} | ||
|
||
@Override | ||
public void stop() | ||
{ | ||
// do nothing | ||
} | ||
}; | ||
// Using Lifecycle.Stage.LAST because DruidNodeDiscoveryProvider should be already started when | ||
// registerSelfDiscoveryListener() is called. | ||
lifecycle.addHandler(selfDiscoveryListenerRegistrator, Lifecycle.Stage.LAST); | ||
} | ||
|
||
private void registerSelfDiscoveryListener( | ||
DruidNode thisDruidNode, | ||
NodeType thisNodeType, | ||
DruidNodeDiscoveryProvider nodeDiscoveryProvider | ||
) | ||
{ | ||
nodeDiscoveryProvider.getForNodeType(thisNodeType).registerListener(new DruidNodeDiscovery.Listener() | ||
{ | ||
@Override | ||
public void nodesAdded(Collection<DiscoveryDruidNode> nodes) | ||
{ | ||
if (selfDiscovered) { | ||
return; | ||
} | ||
for (DiscoveryDruidNode node : nodes) { | ||
if (node.getDruidNode().equals(thisDruidNode)) { | ||
selfDiscovered = true; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void nodesRemoved(Collection<DiscoveryDruidNode> nodes) | ||
{ | ||
// do nothing | ||
} | ||
}); | ||
} | ||
|
||
@GET | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public Response getSelfDiscovered() | ||
{ | ||
return Response.ok(Collections.singletonMap("selfDiscovered", selfDiscovered)).build(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, describe why it's not safe, probably with an example scenario.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, extended docs to describe that.