io.trino.metadata.InternalNodeManager Maven / Gradle / Ivy
/*
* 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.metadata;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSetMultimap;
import com.google.common.collect.SetMultimap;
import io.trino.spi.connector.CatalogHandle;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import static com.google.common.base.MoreObjects.toStringHelper;
import static java.util.Objects.requireNonNull;
public interface InternalNodeManager
{
Set getNodes(NodeState state);
Set getActiveCatalogNodes(CatalogHandle catalogHandle);
NodesSnapshot getActiveNodesSnapshot();
InternalNode getCurrentNode();
Set getCoordinators();
AllNodes getAllNodes();
void refreshNodes();
void addNodeChangeListener(Consumer listener);
void removeNodeChangeListener(Consumer listener);
class NodesSnapshot
{
private final Set allNodes;
private final Optional> connectorNodes;
public NodesSnapshot(Set allActiveNodes, Optional> activeNodesByCatalogName)
{
requireNonNull(allActiveNodes, "allActiveNodes is null");
requireNonNull(activeNodesByCatalogName, "activeNodesByCatalogName is null");
this.allNodes = ImmutableSet.copyOf(allActiveNodes);
this.connectorNodes = activeNodesByCatalogName.map(ImmutableSetMultimap::copyOf);
}
public Set getAllNodes()
{
return allNodes;
}
public Set getConnectorNodes(CatalogHandle catalogHandle)
{
return connectorNodes
.map(map -> map.get(catalogHandle))
.orElse(allNodes);
}
@Override
public String toString()
{
return toStringHelper(this)
.add("allNodes", allNodes)
.add("connectorNodes", connectorNodes.orElse(null))
.toString();
}
}
}