Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Merge pull request #560 from apache/dynamic_config
[IOTDB-247] Optimize Dynamic Config with ActiveTimeSeriesCounter
- Loading branch information
Showing
18 changed files
with
452 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* | ||
* 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.db.conf.adapter; | ||
|
||
import com.clearspring.analytics.stream.cardinality.HyperLogLog; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
import org.apache.iotdb.db.conf.IoTDBConstant; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class ActiveTimeSeriesCounter implements IActiveTimeSeriesCounter { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(ActiveTimeSeriesCounter.class); | ||
/** | ||
* Map[StorageGroup, HyperLogLogCounter] | ||
*/ | ||
private static Map<String, HyperLogLog> storageGroupHllMap = new ConcurrentHashMap<>(); | ||
|
||
/** | ||
* Map[StorageGroup, ActiveTimeSeriesRatio] | ||
*/ | ||
private static Map<String, Double> activeRatioMap = new ConcurrentHashMap<>(); | ||
|
||
/** | ||
* Map[StorageGroup, ActiveTimeSeriesNumber] | ||
*/ | ||
private static Map<String, Long> activeTimeSeriesNumMap = new ConcurrentHashMap<>(); | ||
|
||
/** | ||
* LOG2M decide the precision of the HyperLogLog algorithm | ||
*/ | ||
static final int LOG2M = 13; | ||
|
||
private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); | ||
|
||
@Override | ||
public void init(String storageGroup) { | ||
storageGroupHllMap.put(storageGroup, new HyperLogLog(LOG2M)); | ||
activeRatioMap.put(storageGroup, 0D); | ||
activeTimeSeriesNumMap.put(storageGroup, 0L); | ||
} | ||
|
||
@Override | ||
public void offer(String storageGroup, String device, String measurement) { | ||
String path = device + IoTDBConstant.PATH_SEPARATOR + measurement; | ||
try { | ||
storageGroupHllMap.get(storageGroup).offer(path); | ||
} catch (Exception e) { | ||
storageGroupHllMap.putIfAbsent(storageGroup, new HyperLogLog(LOG2M)); | ||
storageGroupHllMap.get(storageGroup).offer(path); | ||
LOGGER.error("Storage group {} registers active time series {} failed", storageGroup, path, | ||
e); | ||
} | ||
} | ||
|
||
@Override | ||
public void updateActiveRatio(String storageGroup) { | ||
lock.writeLock().lock(); | ||
try { | ||
long activeTimeSeriesNum = storageGroupHllMap.get(storageGroup).cardinality(); | ||
if (activeTimeSeriesNum != activeTimeSeriesNumMap.get(storageGroup)) { | ||
// update the active time series number in the newest memtable to be flushed | ||
activeTimeSeriesNumMap.put(storageGroup, activeTimeSeriesNum); | ||
|
||
double totalActiveTsNum = 0; | ||
LOGGER.debug("{}: updating active ratio", Thread.currentThread().getName()); | ||
for (double number : activeTimeSeriesNumMap.values()) { | ||
totalActiveTsNum += number; | ||
} | ||
for (Map.Entry<String, Long> entry : activeTimeSeriesNumMap.entrySet()) { | ||
double activeRatio = 0; | ||
if (totalActiveTsNum > 0) { | ||
activeRatio = entry.getValue() / totalActiveTsNum; | ||
} | ||
activeRatioMap.put(entry.getKey(), activeRatio); | ||
LOGGER.debug("{}: storage group {} has an active ratio: {}", | ||
Thread.currentThread().getName(), | ||
entry.getKey(), activeRatio); | ||
} | ||
} | ||
// initialize the HLL counter | ||
storageGroupHllMap.put(storageGroup, new HyperLogLog(LOG2M)); | ||
} catch (Exception e) { | ||
LOGGER.error("Update {} active ratio failed", storageGroup, e); | ||
} finally { | ||
lock.writeLock().unlock(); | ||
} | ||
} | ||
|
||
@Override | ||
public double getActiveRatio(String storageGroup) { | ||
lock.writeLock().lock(); | ||
double ratio; | ||
try { | ||
ratio = activeRatioMap.get(storageGroup); | ||
} catch (Exception e) { | ||
ratio = 0; | ||
LOGGER.error("Get active ratio failed", e); | ||
} finally { | ||
lock.writeLock().unlock(); | ||
} | ||
return ratio; | ||
} | ||
|
||
@Override | ||
public void delete(String storageGroup) { | ||
storageGroupHllMap.remove(storageGroup); | ||
activeRatioMap.remove(storageGroup); | ||
activeTimeSeriesNumMap.remove(storageGroup); | ||
} | ||
|
||
private static class ActiveTimeSeriesCounterHolder { | ||
private static final ActiveTimeSeriesCounter INSTANCE = new ActiveTimeSeriesCounter(); | ||
} | ||
|
||
public static ActiveTimeSeriesCounter getInstance() { | ||
return ActiveTimeSeriesCounterHolder.INSTANCE; | ||
} | ||
|
||
/** | ||
* this method is for test | ||
*/ | ||
public static void clear() { | ||
storageGroupHllMap = new ConcurrentHashMap<>(); | ||
activeRatioMap = new ConcurrentHashMap<>(); | ||
activeTimeSeriesNumMap = new ConcurrentHashMap<>(); | ||
} | ||
} |
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,61 @@ | ||
/* | ||
* 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.db.conf.adapter; | ||
|
||
public interface IActiveTimeSeriesCounter { | ||
|
||
/** | ||
* Initialize the counter by adding a new HyperLogLog counter for the given storage group | ||
* | ||
* @param storageGroup the given storage group to be initialized | ||
*/ | ||
void init(String storageGroup); | ||
|
||
/** | ||
* Register a time series to the active time series counter | ||
* | ||
* @param storageGroup the storage group name of the time series | ||
* @param device the device name of the time series | ||
* @param measurement the sensor name of the time series | ||
*/ | ||
void offer(String storageGroup, String device, String measurement); | ||
|
||
/** | ||
* Update the ActiveRatioMap | ||
* | ||
* @param storageGroup whose counter will be refreshed after the update | ||
*/ | ||
void updateActiveRatio(String storageGroup); | ||
|
||
/** | ||
* Get the active time series number proportion of the given storage group | ||
* | ||
* @param storageGroup the storage group to be calculated | ||
* @return the active time series number proportion of the given storage group | ||
*/ | ||
double getActiveRatio(String storageGroup); | ||
|
||
/** | ||
* Delete the counter for the given storage group | ||
* | ||
* @param storageGroup whose counter will be removed | ||
*/ | ||
void delete(String storageGroup); | ||
|
||
} |
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
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
Oops, something went wrong.