-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor code by adding ServerInfoCache
- Loading branch information
Showing
4 changed files
with
109 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
pinot-controller/src/main/java/org/apache/pinot/controller/util/ServerInfoCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/** | ||
* 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.util; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.apache.helix.model.InstanceConfig; | ||
import org.apache.helix.zookeeper.datamodel.ZNRecord; | ||
import org.apache.pinot.controller.helix.core.PinotHelixResourceManager; | ||
import org.apache.pinot.spi.utils.CommonConstants; | ||
import org.apache.pinot.spi.utils.InstanceTypeUtils; | ||
|
||
|
||
/** | ||
* This is a helper class maintaining a cache of server information. It is used to avoid repeated calls to Helix to | ||
* get server information. This class is not thread safe. | ||
*/ | ||
public class ServerInfoCache { | ||
PinotHelixResourceManager pinotHelixResourceManager; | ||
Map<String, ServerInfo> serverInfoMap; | ||
|
||
public ServerInfoCache(PinotHelixResourceManager pinotHelixResourceManager) { | ||
this.pinotHelixResourceManager = pinotHelixResourceManager; | ||
this.serverInfoMap = new HashMap<>(); | ||
} | ||
|
||
public ServerInfo getServerInfo(String instanceId) { | ||
return serverInfoMap.computeIfAbsent(instanceId, this::getServerInfoOndemand); | ||
} | ||
|
||
private ServerInfo getServerInfoOndemand(String instanceId) { | ||
InstanceConfig instanceConfig = pinotHelixResourceManager.getHelixInstanceConfig(instanceId); | ||
if (instanceConfig == null || !InstanceTypeUtils.isServer(instanceId)) { | ||
return null; | ||
} | ||
List<String> tags = instanceConfig.getTags(); | ||
ZNRecord record = instanceConfig.getRecord(); | ||
boolean helixEnabled = record.getBooleanField( | ||
InstanceConfig.InstanceConfigProperty.HELIX_ENABLED.name(), false); | ||
boolean queriesDisabled = record.getBooleanField(CommonConstants.Helix.QUERIES_DISABLED, false); | ||
boolean shutdownInProgress = record.getBooleanField(CommonConstants.Helix.IS_SHUTDOWN_IN_PROGRESS, false); | ||
return new ServerInfo(instanceId, tags, null, helixEnabled, queriesDisabled, shutdownInProgress); | ||
} | ||
|
||
public static class ServerInfo { | ||
public String instanceName; | ||
public List<String> tags; | ||
public List<String> tables; | ||
public boolean helixEnabled; | ||
public boolean queriesDisabled; | ||
public boolean shutdownInProgress; | ||
public ServerInfo(String instanceName, | ||
List<String> tags, | ||
List<String> tables, | ||
boolean helixEnabled, | ||
boolean queriesDisabled, | ||
boolean shutdownInProgress) { | ||
this.instanceName = instanceName; | ||
this.tags = tags; | ||
this.tables = tables; | ||
this.helixEnabled = helixEnabled; | ||
this.queriesDisabled = queriesDisabled; | ||
this.shutdownInProgress = shutdownInProgress; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters