-
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
161 additions
and
75 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
131 changes: 131 additions & 0 deletions
131
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,131 @@ | ||
/** | ||
* 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 { | ||
private String _instanceName; | ||
private List<String> _tags; | ||
private List<String> _tables; | ||
private boolean _helixEnabled; | ||
private boolean _queriesDisabled; | ||
private boolean _shutdownInProgress; | ||
private ServerInfo(String instanceName, | ||
List<String> tags, | ||
List<String> tables, | ||
boolean helixEnabled, | ||
boolean queriesDisabled, | ||
boolean shutdownInProgress) { | ||
_instanceName = instanceName; | ||
_tags = tags; | ||
_tables = tables; | ||
_helixEnabled = helixEnabled; | ||
_queriesDisabled = queriesDisabled; | ||
_shutdownInProgress = shutdownInProgress; | ||
} | ||
|
||
public String getInstanceName() { | ||
return _instanceName; | ||
} | ||
|
||
public void setInstanceName(String instanceName) { | ||
_instanceName = instanceName; | ||
} | ||
|
||
public List<String> getTags() { | ||
return _tags; | ||
} | ||
|
||
public void setTags(List<String> tags) { | ||
_tags = tags; | ||
} | ||
|
||
public List<String> getTables() { | ||
return _tables; | ||
} | ||
|
||
public void setTables(List<String> tables) { | ||
_tables = tables; | ||
} | ||
|
||
public boolean isHelixEnabled() { | ||
return _helixEnabled; | ||
} | ||
|
||
public void setHelixEnabled(boolean helixEnabled) { | ||
_helixEnabled = helixEnabled; | ||
} | ||
|
||
public boolean isQueriesDisabled() { | ||
return _queriesDisabled; | ||
} | ||
|
||
public void setQueriesDisabled(boolean queriesDisabled) { | ||
_queriesDisabled = queriesDisabled; | ||
} | ||
|
||
public boolean isShutdownInProgress() { | ||
return _shutdownInProgress; | ||
} | ||
|
||
public void setShutdownInProgress(boolean shutdownInProgress) { | ||
_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