Skip to content

Commit

Permalink
[to rel/1.0] Add show quota (apache#363)
Browse files Browse the repository at this point in the history
  • Loading branch information
RYH61 committed Mar 3, 2023
1 parent 5db1c7d commit dbb1049
Show file tree
Hide file tree
Showing 27 changed files with 562 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ ddlStatement
| showFunctions | showTriggers | showContinuousQueries | showTTL | showAllTTL | showCluster | showVariables | showRegion | showDataNodes | showConfigNodes
| showSchemaTemplates | showNodesInSchemaTemplate
| showPathsUsingSchemaTemplate | showPathsSetSchemaTemplate
| showSpaceQuota | showThrottleQuota
| showSpaceQuota | showThrottleQuota | showSpaceResource
| countStorageGroup | countDevices | countTimeseries | countNodes
| getRegionId | getTimeSlotList | getSeriesSlotList
;
Expand Down Expand Up @@ -402,6 +402,11 @@ showThrottleQuota
: SHOW THROTTLE QUOTA (userName=identifier)?
;

// Show Space Resource
showSpaceResource
: SHOW SPACE RESOURCE
;

// Count Storage Group
countStorageGroup
: COUNT (STORAGE GROUP | DATABASES) prefixPath?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,6 @@ public enum DataNodeRequestType {

/** Quota */
SET_SPACE_QUOTA,
SET_THROTTLE_QUOTA
SET_THROTTLE_QUOTA,
GET_SPACE_RESOURCE
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.iotdb.confignode.client.async.handlers.rpc.CountPathsUsingTemplateRPCHandler;
import org.apache.iotdb.confignode.client.async.handlers.rpc.DeleteSchemaRPCHandler;
import org.apache.iotdb.confignode.client.async.handlers.rpc.FetchSchemaBlackListRPCHandler;
import org.apache.iotdb.confignode.client.async.handlers.rpc.GetSpaceResourceRPCHandler;
import org.apache.iotdb.mpp.rpc.thrift.TActiveTriggerInstanceReq;
import org.apache.iotdb.mpp.rpc.thrift.TConstructSchemaBlackListReq;
import org.apache.iotdb.mpp.rpc.thrift.TConstructSchemaBlackListWithTemplateReq;
Expand Down Expand Up @@ -338,6 +339,11 @@ private void sendAsyncRequestToDataNode(
(AsyncTSStatusRPCHandler)
clientHandler.createAsyncRPCHandler(requestId, targetDataNode));
break;
case GET_SPACE_RESOURCE:
client.getSpaceResource(
(GetSpaceResourceRPCHandler)
clientHandler.createAsyncRPCHandler(requestId, targetDataNode));
break;
default:
LOGGER.error(
"Unexpected DataNode Request Type: {} when sendAsyncRequestToDataNode",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
import org.apache.iotdb.confignode.client.async.handlers.rpc.CountPathsUsingTemplateRPCHandler;
import org.apache.iotdb.confignode.client.async.handlers.rpc.DeleteSchemaRPCHandler;
import org.apache.iotdb.confignode.client.async.handlers.rpc.FetchSchemaBlackListRPCHandler;
import org.apache.iotdb.confignode.client.async.handlers.rpc.GetSpaceResourceRPCHandler;
import org.apache.iotdb.confignode.client.async.handlers.rpc.OperatePipeRPCHandler;
import org.apache.iotdb.mpp.rpc.thrift.TCountPathsUsingTemplateResp;
import org.apache.iotdb.mpp.rpc.thrift.TFetchSchemaBlackListResp;
import org.apache.iotdb.mpp.rpc.thrift.TSpaceResourceResp;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -194,6 +196,14 @@ public AbstractAsyncRPCHandler<?> createAsyncRPCHandler(
dataNodeLocationMap,
(Map<Integer, TCountPathsUsingTemplateResp>) responseMap,
countDownLatch);
case GET_SPACE_RESOURCE:
return new GetSpaceResourceRPCHandler(
requestType,
requestId,
targetDataNode,
dataNodeLocationMap,
(Map<Integer, TSpaceResourceResp>) responseMap,
countDownLatch);
case SET_TTL:
case CREATE_DATA_REGION:
case CREATE_SCHEMA_REGION:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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.iotdb.confignode.client.async.handlers.rpc;

import org.apache.iotdb.common.rpc.thrift.TDataNodeLocation;
import org.apache.iotdb.common.rpc.thrift.TSStatus;
import org.apache.iotdb.confignode.client.DataNodeRequestType;
import org.apache.iotdb.mpp.rpc.thrift.TSpaceResourceResp;
import org.apache.iotdb.rpc.RpcUtils;
import org.apache.iotdb.rpc.TSStatusCode;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.concurrent.CountDownLatch;

public class GetSpaceResourceRPCHandler extends AbstractAsyncRPCHandler<TSpaceResourceResp> {

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

public GetSpaceResourceRPCHandler(
DataNodeRequestType requestType,
int requestId,
TDataNodeLocation targetDataNode,
Map<Integer, TDataNodeLocation> dataNodeLocationMap,
Map<Integer, TSpaceResourceResp> responseMap,
CountDownLatch countDownLatch) {
super(requestType, requestId, targetDataNode, dataNodeLocationMap, responseMap, countDownLatch);
}

@Override
public void onComplete(TSpaceResourceResp response) {
TSStatus tsStatus = response.getStatus();
responseMap.put(requestId, response);
if (tsStatus.getCode() == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
dataNodeLocationMap.remove(requestId);
LOGGER.info("Successfully get space resource on DataNode: {}", targetDataNode);
} else if (tsStatus.getCode() == TSStatusCode.MULTIPLE_ERROR.getStatusCode()) {
dataNodeLocationMap.remove(requestId);
LOGGER.error("Failed to get space resource on DataNode {}, {}", targetDataNode, tsStatus);
} else {
LOGGER.error("Failed to get space resource on DataNode {}, {}", targetDataNode, tsStatus);
}
countDownLatch.countDown();
}

@Override
public void onError(Exception exception) {
String errorMsg =
"Get space resource error on DataNode: {id="
+ targetDataNode.getDataNodeId()
+ ", internalEndPoint="
+ targetDataNode.getInternalEndPoint()
+ "}"
+ exception.getMessage();
LOGGER.error(errorMsg);

countDownLatch.countDown();
TSpaceResourceResp resp = new TSpaceResourceResp();
resp.setStatus(
new TSStatus(
RpcUtils.getStatus(TSStatusCode.EXECUTE_STATEMENT_ERROR.getStatusCode(), errorMsg)));
responseMap.put(requestId, resp);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,22 @@
import org.apache.iotdb.common.rpc.thrift.TThrottleQuota;
import org.apache.iotdb.commons.conf.CommonConfig;
import org.apache.iotdb.commons.conf.CommonDescriptor;
import org.apache.iotdb.commons.conf.IoTDBConstant;
import org.apache.iotdb.commons.quotas.SpaceQuotaType;
import org.apache.iotdb.confignode.client.DataNodeRequestType;
import org.apache.iotdb.confignode.client.async.AsyncDataNodeClientPool;
import org.apache.iotdb.confignode.client.async.handlers.AsyncClientHandler;
import org.apache.iotdb.confignode.conf.ConfigNodeDescriptor;
import org.apache.iotdb.confignode.consensus.request.write.quota.SetSpaceQuotaPlan;
import org.apache.iotdb.confignode.consensus.request.write.quota.SetThrottleQuotaPlan;
import org.apache.iotdb.confignode.manager.partition.PartitionManager;
import org.apache.iotdb.confignode.persistence.quota.QuotaInfo;
import org.apache.iotdb.confignode.rpc.thrift.TShowSpaceResourceResp;
import org.apache.iotdb.confignode.rpc.thrift.TShowThrottleReq;
import org.apache.iotdb.confignode.rpc.thrift.TSpaceQuotaResp;
import org.apache.iotdb.confignode.rpc.thrift.TThrottleQuotaResp;
import org.apache.iotdb.consensus.common.response.ConsensusWriteResponse;
import org.apache.iotdb.mpp.rpc.thrift.TSpaceResourceResp;
import org.apache.iotdb.rpc.RpcUtils;
import org.apache.iotdb.rpc.TSStatusCode;

Expand Down Expand Up @@ -205,6 +210,91 @@ public TThrottleQuotaResp getThrottleQuota() {
return throttleQuotaResp;
}

public TShowSpaceResourceResp showSpaceResource() {
TShowSpaceResourceResp showSpaceResourceResp = new TShowSpaceResourceResp();
// get resource
Map<Integer, TDataNodeLocation> dataNodeLocationMap =
configManager.getNodeManager().getRegisteredDataNodeLocations();
AsyncClientHandler<Object, TSpaceResourceResp> clientHandler =
new AsyncClientHandler<>(DataNodeRequestType.GET_SPACE_RESOURCE, dataNodeLocationMap);
AsyncDataNodeClientPool.getInstance().sendAsyncRequestToDataNodeWithRetry(clientHandler);
List<TSpaceResourceResp> responseList = clientHandler.getResponseList();
long totalSpace = 0;
long freeSpace = 0;
long maxMemory = 0;
for (TSpaceResourceResp spaceResourceResp : responseList) {
if (spaceResourceResp.getStatus().getCode() == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
totalSpace += spaceResourceResp.getTotalSpace();
freeSpace += spaceResourceResp.getFreeSpace();
maxMemory += spaceResourceResp.getMaxMemory();
}
}
Map<String, Map<String, Long>> spaceResource = new HashMap<>();
Map<String, Long> resource = new HashMap<>();
// Allocated
long deviceNumAllocated = 0;
long timeSeriesNumAllocated = 0;
long diskSizeAllocated = 0;
// Used
long diskSizeUsed = 0;
if (!quotaInfo.getSpaceQuotaLimit().isEmpty()) {
deviceNumAllocated =
quotaInfo.getSpaceQuotaLimit().values().stream()
.mapToLong(TSpaceQuota::getDeviceNum)
.sum();
timeSeriesNumAllocated =
quotaInfo.getSpaceQuotaLimit().values().stream()
.mapToLong(TSpaceQuota::getTimeserieNum)
.sum();
// diskSize Unit : M
diskSizeAllocated =
quotaInfo.getSpaceQuotaLimit().values().stream().mapToLong(TSpaceQuota::getDiskSize).sum()
/ 1024;
diskSizeUsed =
quotaInfo.getSpaceQuotaUsage().values().stream().mapToLong(TSpaceQuota::getDiskSize).sum()
/ 1024;
}

// device num
long deviceNum =
maxMemory
/ 20
/ (180 + 2 * 100)
/ ConfigNodeDescriptor.getInstance().getConf().getSchemaReplicationFactor();

resource.put(IoTDBConstant.TOTAL, deviceNum);
resource.put(IoTDBConstant.NON_USED, 0L);
resource.put(IoTDBConstant.ALLOCATED, deviceNumAllocated);
resource.put(IoTDBConstant.AVAILABLE, deviceNum - deviceNumAllocated);
resource.put(IoTDBConstant.USED, 0L);
spaceResource.put(SpaceQuotaType.deviceNum.name(), resource);

// TimeSeries num
resource = new HashMap<>();
resource.put(IoTDBConstant.TOTAL, deviceNum);
resource.put(IoTDBConstant.NON_USED, 0L);
resource.put(IoTDBConstant.ALLOCATED, timeSeriesNumAllocated);
resource.put(IoTDBConstant.AVAILABLE, deviceNum - timeSeriesNumAllocated);
resource.put(IoTDBConstant.USED, 0L);
spaceResource.put(SpaceQuotaType.timeSeriesNum.name(), resource);

// Disk Size
resource = new HashMap<>();
// totalSpace, freeSpace Unit : B
totalSpace = totalSpace / 1024 / 1024 / 1024;
freeSpace = freeSpace / 1024 / 1024 / 1024;
resource.put(IoTDBConstant.TOTAL, totalSpace);
resource.put(IoTDBConstant.NON_USED, totalSpace - freeSpace - diskSizeUsed);
resource.put(IoTDBConstant.ALLOCATED, diskSizeAllocated);
resource.put(IoTDBConstant.AVAILABLE, freeSpace + diskSizeUsed - diskSizeAllocated);
resource.put(IoTDBConstant.USED, diskSizeUsed);
spaceResource.put(SpaceQuotaType.diskSize.name(), resource);

showSpaceResourceResp.setSpaceResource(spaceResource);
showSpaceResourceResp.setStatus(RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS));
return showSpaceResourceResp;
}

public boolean hasSpaceQuotaLimit() {
return quotaInfo.getSpaceQuotaLimit().keySet().isEmpty();
}
Expand Down Expand Up @@ -278,7 +368,12 @@ public void updateSpaceQuotaUsage() {
regionDiskCount.addAndGet(regionDisk.get(dataRegionId));
}
});
quotaInfo.getSpaceQuotaUsage().get(entry.getKey()).setDiskSize(regionDiskCount.get());
quotaInfo
.getSpaceQuotaUsage()
.get(entry.getKey())
.setDiskSize(
regionDiskCount.get()
* ConfigNodeDescriptor.getInstance().getConf().getDataReplicationFactor());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
import org.apache.iotdb.confignode.rpc.thrift.TShowDataNodesResp;
import org.apache.iotdb.confignode.rpc.thrift.TShowPipeReq;
import org.apache.iotdb.confignode.rpc.thrift.TShowPipeResp;
import org.apache.iotdb.confignode.rpc.thrift.TShowSpaceResourceResp;
import org.apache.iotdb.confignode.rpc.thrift.TShowStorageGroupResp;
import org.apache.iotdb.confignode.rpc.thrift.TShowThrottleReq;
import org.apache.iotdb.confignode.rpc.thrift.TShowVariablesResp;
Expand Down Expand Up @@ -1550,6 +1551,13 @@ public TThrottleQuotaResp showThrottleQuota(TShowThrottleReq req) {
: new TThrottleQuotaResp(status);
}

public TShowSpaceResourceResp showSpaceResource() {
TSStatus status = confirmLeader();
return status.getCode() == TSStatusCode.SUCCESS_STATUS.getStatusCode()
? clusterQuotaManager.showSpaceResource()
: new TShowSpaceResourceResp(status);
}

/** Get all related schemaRegion which may contains the timeSeries matched by given patternTree */
public Map<TConsensusGroupId, TRegionReplicaSet> getRelatedSchemaRegionGroup(
PathPatternTree patternTree) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class QuotaInfo implements SnapshotProcessor {
private static final Logger logger = LoggerFactory.getLogger(QuotaInfo.class);

private final ReentrantReadWriteLock spaceQuotaReadWriteLock;
// diskSize unit : M
private final Map<String, TSpaceQuota> spaceQuotaLimit;
private final Map<String, TSpaceQuota> spaceQuotaUsage;
private final Map<String, TThrottleQuota> throttleQuotaLimit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
import org.apache.iotdb.confignode.rpc.thrift.TShowPipeResp;
import org.apache.iotdb.confignode.rpc.thrift.TShowRegionReq;
import org.apache.iotdb.confignode.rpc.thrift.TShowRegionResp;
import org.apache.iotdb.confignode.rpc.thrift.TShowSpaceResourceResp;
import org.apache.iotdb.confignode.rpc.thrift.TShowStorageGroupResp;
import org.apache.iotdb.confignode.rpc.thrift.TShowThrottleReq;
import org.apache.iotdb.confignode.rpc.thrift.TShowVariablesResp;
Expand Down Expand Up @@ -847,4 +848,9 @@ public TThrottleQuotaResp showThrottleQuota(TShowThrottleReq req) throws TExcept
public TThrottleQuotaResp getThrottleQuota() throws TException {
return configManager.getThrottleQuota();
}

@Override
public TShowSpaceResourceResp showSpaceResource() throws TException {
return configManager.showSpaceResource();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.apache.iotdb.db.it.quotas;

import org.apache.iotdb.commons.conf.CommonDescriptor;
import org.apache.iotdb.it.env.EnvFactory;
import org.apache.iotdb.it.framework.IoTDBTestRunner;
import org.apache.iotdb.itbase.category.ClusterIT;
Expand All @@ -9,6 +8,7 @@
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
Expand All @@ -26,17 +26,18 @@

@RunWith(IoTDBTestRunner.class)
@Category({LocalStandaloneIT.class, ClusterIT.class})
@Ignore
public class IoTDBQuotaIT {

@Before
public void setUp() throws Exception {
EnvFactory.getEnv().initBeforeTest();
CommonDescriptor.getInstance().getConfig().setQuotaEnable(true);
// CommonDescriptor.getInstance().getConfig().setQuotaEnable(true);
}

@After
public void tearDown() throws Exception {
CommonDescriptor.getInstance().getConfig().setQuotaEnable(false);
// CommonDescriptor.getInstance().getConfig().setQuotaEnable(false);
EnvFactory.getEnv().cleanAfterTest();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,11 @@ private IoTDBConstant() {}
public static final String REQUEST_TYPE_READ = "read";
public static final String REQUEST_TYPE_WRITE = "write";
public static final String REQ_UNIT = "req";
public static final String TOTAL = "Total";
public static final String NON_USED = "Non-Times-Used";
public static final String ALLOCATED = "Allocated";
public static final String AVAILABLE = "Available";
public static final String USED = "Times-Used";

// client version number
public enum ClientVersion {
Expand Down

0 comments on commit dbb1049

Please sign in to comment.