Skip to content

Commit

Permalink
Refactor last flush time management (apache#11946)
Browse files Browse the repository at this point in the history
  • Loading branch information
HTHou authored Jan 28, 2024
1 parent 769a151 commit e7b0ca5
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 193 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ public DataRegion(
logger.error("create database system Directory {} failed", storageGroupSysDir.getPath());
}

lastFlushTimeMap = new HashLastFlushTimeMap(tsFileManager);
lastFlushTimeMap = new HashLastFlushTimeMap();

// recover tsfiles unless consensus protocol is ratis and storage storageengine is not ready
if (config.isClusterMode()
Expand Down Expand Up @@ -2732,7 +2732,7 @@ private long getAndSetNewVersion(long timePartitionId, TsFileResource tsFileReso
* Update latest time in latestTimeForEachDevice and
* partitionLatestFlushedTimeForEachDevice. @UsedBy sync module, load external tsfile module.
*/
private void updateLastFlushTime(TsFileResource newTsFileResource) {
protected void updateLastFlushTime(TsFileResource newTsFileResource) {
for (String device : newTsFileResource.getDevices()) {
long endTime = newTsFileResource.getEndTime(device);
long timePartitionId = TimePartitionUtils.getTimePartitionId(endTime);
Expand Down Expand Up @@ -3170,7 +3170,8 @@ public void insert(InsertRowsNode insertRowsNode)
// init map
timePartitionIds[i] = TimePartitionUtils.getTimePartitionId(insertRowNode.getTime());

if (!lastFlushTimeMap.checkAndCreateFlushedTimePartition(timePartitionIds[i])) {
if (config.isEnableSeparateData()
&& !lastFlushTimeMap.checkAndCreateFlushedTimePartition(timePartitionIds[i])) {
TimePartitionManager.getInstance()
.registerTimePartitionInfo(
new TimePartitionInfo(
Expand Down Expand Up @@ -3398,13 +3399,9 @@ public void markDeleted() {
}
}

public void releaseFlushTimeMap(long timePartitionId) {
writeLock("releaseFlushTimeMap");
try {
lastFlushTimeMap.removePartition(timePartitionId);
} finally {
writeUnlock();
}
/* Be careful, the thread that calls this method may not hold the write lock!!*/
public void degradeFlushTimeMap(long timePartitionId) {
lastFlushTimeMap.degradeLastFlushTime(timePartitionId);
}

public long getMemCost() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.storageengine.dataregion;

import java.util.HashMap;
import java.util.Map;

public class DeviceLastFlushTime implements ILastFlushTime {

Map<String, Long> deviceLastFlushTimeMap = new HashMap<>();

@Override
public long getLastFlushTime(String deviceId) {
if (deviceLastFlushTimeMap.containsKey(deviceId)) {
return deviceLastFlushTimeMap.get(deviceId);
}
return Long.MIN_VALUE;
}

@Override
public void updateLastFlushTime(String deviceId, long time) {
deviceLastFlushTimeMap.merge(deviceId, time, Math::max);
}

@Override
public ILastFlushTime degradeLastFlushTime() {
long maxTime = Long.MIN_VALUE;
for (long time : deviceLastFlushTimeMap.values()) {
maxTime = Math.max(maxTime, time);
}
return new PartitionLastFlushTime(maxTime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@

package org.apache.iotdb.db.storageengine.dataregion;

import org.apache.iotdb.db.storageengine.dataregion.tsfile.TsFileManager;

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

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class HashLastFlushTimeMap implements ILastFlushTimeMap {

Expand Down Expand Up @@ -57,8 +55,7 @@ public class HashLastFlushTimeMap implements ILastFlushTimeMap {
*
* <p>It is used to separate sequence and unsequence data.
*/
private final Map<Long, Map<String, Long>> partitionLatestFlushedTimeForEachDevice =
new HashMap<>();
private final Map<Long, ILastFlushTime> partitionLatestFlushedTime = new ConcurrentHashMap<>();

/**
* global mapping of device -> largest timestamp of the latest memtable to * be submitted to
Expand All @@ -68,52 +65,39 @@ public class HashLastFlushTimeMap implements ILastFlushTimeMap {
*
* <p>It is used to update last cache.
*/
private final Map<String, Long> globalLatestFlushedTimeForEachDevice = new HashMap<>();

/** used for recovering flush time from tsfile resource */
TsFileManager tsFileManager;
private final Map<String, Long> globalLatestFlushedTimeForEachDevice = new ConcurrentHashMap<>();

/** record memory cost of map for each partitionId */
private final Map<Long, Long> memCostForEachPartition = new HashMap<>();

public HashLastFlushTimeMap(TsFileManager tsFileManager) {
this.tsFileManager = tsFileManager;
}
private final Map<Long, Long> memCostForEachPartition = new ConcurrentHashMap<>();

// For load
@Override
public void updateOneDeviceFlushedTime(long timePartitionId, String path, long time) {
Map<String, Long> flushTimeMapForPartition =
partitionLatestFlushedTimeForEachDevice.computeIfAbsent(
timePartitionId, id -> new HashMap<>());

flushTimeMapForPartition.compute(
path,
(k, v) -> {
if (v == null) {
v = recoverFlushTime(timePartitionId, path);
}
if (v == Long.MIN_VALUE) {
long memCost = HASHMAP_NODE_BASIC_SIZE + 2L * path.length();
memCostForEachPartition.compute(
timePartitionId, (k1, v1) -> v1 == null ? memCost : v1 + memCost);
return time;
}
return Math.max(v, time);
});
public void updateOneDeviceFlushedTime(long timePartitionId, String deviceId, long time) {
ILastFlushTime flushTimeMapForPartition =
partitionLatestFlushedTime.computeIfAbsent(
timePartitionId, id -> new DeviceLastFlushTime());
long lastFlushTime = flushTimeMapForPartition.getLastFlushTime(deviceId);
if (lastFlushTime == Long.MIN_VALUE) {
long memCost = HASHMAP_NODE_BASIC_SIZE + 2L * deviceId.length();
memCostForEachPartition.compute(
timePartitionId, (k1, v1) -> v1 == null ? memCost : v1 + memCost);
}
flushTimeMapForPartition.updateLastFlushTime(deviceId, time);
}

// For recover
@Override
public void updateMultiDeviceFlushedTime(long timePartitionId, Map<String, Long> flushedTimeMap) {
Map<String, Long> flushTimeMapForPartition =
partitionLatestFlushedTimeForEachDevice.computeIfAbsent(
timePartitionId, id -> new HashMap<>());
ILastFlushTime flushTimeMapForPartition =
partitionLatestFlushedTime.computeIfAbsent(
timePartitionId, id -> new DeviceLastFlushTime());

long memIncr = 0;
for (Map.Entry<String, Long> entry : flushedTimeMap.entrySet()) {
if (!flushTimeMapForPartition.containsKey(entry.getKey())) {
if (flushTimeMapForPartition.getLastFlushTime(entry.getKey()) == Long.MIN_VALUE) {
memIncr += HASHMAP_NODE_BASIC_SIZE + 2L * entry.getKey().length();
}
flushTimeMapForPartition.merge(entry.getKey(), entry.getValue(), Math::max);
flushTimeMapForPartition.updateLastFlushTime(entry.getKey(), entry.getValue());
}
long finalMemIncr = memIncr;
memCostForEachPartition.compute(
Expand All @@ -135,19 +119,20 @@ public void updateMultiDeviceGlobalFlushedTime(Map<String, Long> globalFlushedTi

@Override
public boolean checkAndCreateFlushedTimePartition(long timePartitionId) {
if (!partitionLatestFlushedTimeForEachDevice.containsKey(timePartitionId)) {
partitionLatestFlushedTimeForEachDevice.put(timePartitionId, new HashMap<>());
if (!partitionLatestFlushedTime.containsKey(timePartitionId)) {
partitionLatestFlushedTime.put(timePartitionId, new DeviceLastFlushTime());
return false;
}
return true;
}

// For insert
@Override
public void updateLatestFlushTime(long partitionId, Map<String, Long> updateMap) {
for (Map.Entry<String, Long> entry : updateMap.entrySet()) {
partitionLatestFlushedTimeForEachDevice
.computeIfAbsent(partitionId, id -> new HashMap<>())
.merge(entry.getKey(), entry.getValue(), Math::max);
partitionLatestFlushedTime
.computeIfAbsent(partitionId, id -> new DeviceLastFlushTime())
.updateLastFlushTime(entry.getKey(), entry.getValue());
if (globalLatestFlushedTimeForEachDevice.getOrDefault(entry.getKey(), Long.MIN_VALUE)
< entry.getValue()) {
globalLatestFlushedTimeForEachDevice.put(entry.getKey(), entry.getValue());
Expand All @@ -156,10 +141,8 @@ public void updateLatestFlushTime(long partitionId, Map<String, Long> updateMap)
}

@Override
public long getFlushedTime(long timePartitionId, String path) {
return partitionLatestFlushedTimeForEachDevice
.get(timePartitionId)
.computeIfAbsent(path, k -> recoverFlushTime(timePartitionId, path));
public long getFlushedTime(long timePartitionId, String deviceId) {
return partitionLatestFlushedTime.get(timePartitionId).getLastFlushTime(deviceId);
}

@Override
Expand All @@ -169,7 +152,7 @@ public long getGlobalFlushedTime(String path) {

@Override
public void clearFlushedTime() {
partitionLatestFlushedTimeForEachDevice.clear();
partitionLatestFlushedTime.clear();
}

@Override
Expand All @@ -178,15 +161,10 @@ public void clearGlobalFlushedTime() {
}

@Override
public void removePartition(long partitionId) {
partitionLatestFlushedTimeForEachDevice.remove(partitionId);
memCostForEachPartition.remove(partitionId);
}

private long recoverFlushTime(long partitionId, String devicePath) {
long memCost = HASHMAP_NODE_BASIC_SIZE + 2L * devicePath.length();
memCostForEachPartition.compute(partitionId, (k, v) -> v == null ? memCost : v + memCost);
return tsFileManager.recoverFlushTimeFromTsFileResource(partitionId, devicePath);
public void degradeLastFlushTime(long partitionId) {
partitionLatestFlushedTime.computeIfPresent(
partitionId, (id, lastFlushTime) -> lastFlushTime.degradeLastFlushTime());
memCostForEachPartition.put(partitionId, (long) Long.BYTES);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.storageengine.dataregion;

public interface ILastFlushTime {

long getLastFlushTime(String device);

void updateLastFlushTime(String device, long time);

ILastFlushTime degradeLastFlushTime();
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
public interface ILastFlushTimeMap {

// region update
/** Update partitionLatestFlushedTimeForEachDevice. */
void updateOneDeviceFlushedTime(long timePartitionId, String path, long time);
/** Update partitionLatestFlushedTime. */
void updateOneDeviceFlushedTime(long timePartitionId, String deviceId, long time);

void updateMultiDeviceFlushedTime(long timePartitionId, Map<String, Long> flushedTimeMap);

Expand All @@ -35,9 +35,7 @@ public interface ILastFlushTimeMap {

void updateMultiDeviceGlobalFlushedTime(Map<String, Long> globalFlushedTimeMap);

/**
* Update both partitionLatestFlushedTimeForEachDevice and globalLatestFlushedTimeForEachDevice.
*/
/** Update both partitionLatestFlushedTime and globalLatestFlushedTimeForEachDevice. */
void updateLatestFlushTime(long partitionId, Map<String, Long> updateMap);
// endregion

Expand All @@ -47,7 +45,7 @@ public interface ILastFlushTimeMap {
// endregion

// region read
long getFlushedTime(long timePartitionId, String path);
long getFlushedTime(long timePartitionId, String deviceId);

long getGlobalFlushedTime(String path);
// endregion
Expand All @@ -58,7 +56,7 @@ public interface ILastFlushTimeMap {
void clearGlobalFlushedTime();
// endregion

void removePartition(long partitionId);
void degradeLastFlushTime(long partitionId);

long getMemSize(long partitionId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.storageengine.dataregion;

public class PartitionLastFlushTime implements ILastFlushTime {

private long partitionLastFlushTime;

public PartitionLastFlushTime(long initPartitionLastFlushTime) {
this.partitionLastFlushTime = initPartitionLastFlushTime;
}

@Override
public long getLastFlushTime(String device) {
return partitionLastFlushTime;
}

@Override
public void updateLastFlushTime(String device, long time) {
partitionLastFlushTime = Math.max(partitionLastFlushTime, time);
}

@Override
public ILastFlushTime degradeLastFlushTime() {
return this;
}
}
Loading

0 comments on commit e7b0ca5

Please sign in to comment.