Skip to content

Commit

Permalink
[Table Model] Implement table device cache (#12702)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcosZyk authored Jun 11, 2024
1 parent cfb83e5 commit 532e743
Show file tree
Hide file tree
Showing 6 changed files with 371 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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.queryengine.plan.relational.metadata.fetcher.cache;

import org.apache.iotdb.commons.schema.MemUsageUtil;

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

public class TableDeviceCacheEntry {

private final Map<String, String> attributeMap;

public TableDeviceCacheEntry() {
attributeMap = new ConcurrentHashMap<>();
}

public TableDeviceCacheEntry(Map<String, String> attributeMap) {
this.attributeMap = new HashMap<>(attributeMap);
}

public String getAttribute(String key) {
return attributeMap.get(key);
}

public Map<String, String> getAttributeMap() {
return attributeMap;
}

public int estimateSize() {
int size = 8; // map reference
for (Map.Entry<String, String> entry : attributeMap.entrySet()) {
size += (int) MemUsageUtil.computeKVMemUsageInMap(entry.getKey(), entry.getValue());
}
return size;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.queryengine.plan.relational.metadata.fetcher.cache;

import org.apache.iotdb.commons.schema.MemUsageUtil;

import java.util.Arrays;

public class TableDeviceId {

private final String[] idValues;

public TableDeviceId(String[] idValues) {
this.idValues = idValues;
}

public String getIdValue(int index) {
return idValues[index];
}

public String[] getIdValues() {
return idValues;
}

public int estimateSize() {
int size = 8 + 8 + 8 + 4; // object header + reference + String[] header + String.length
for (String node : idValues) {
size += (int) MemUsageUtil.computeStringMemUsage(node);
}
return size;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof TableDeviceId)) return false;
TableDeviceId that = (TableDeviceId) o;
return Arrays.equals(idValues, that.idValues);
}

@Override
public int hashCode() {
return Arrays.hashCode(idValues);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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.queryengine.plan.relational.metadata.fetcher.cache;

import org.apache.iotdb.db.conf.IoTDBConfig;
import org.apache.iotdb.db.conf.IoTDBDescriptor;
import org.apache.iotdb.db.queryengine.plan.analyze.cache.schema.dualkeycache.IDualKeyCache;
import org.apache.iotdb.db.queryengine.plan.analyze.cache.schema.dualkeycache.impl.DualKeyCacheBuilder;
import org.apache.iotdb.db.queryengine.plan.analyze.cache.schema.dualkeycache.impl.DualKeyCachePolicy;

import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class TableDeviceSchemaCache {

private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();

private final IDualKeyCache<TableId, TableDeviceId, TableDeviceCacheEntry> dualKeyCache;

private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock(false);

public TableDeviceSchemaCache() {
DualKeyCacheBuilder<TableId, TableDeviceId, TableDeviceCacheEntry> dualKeyCacheBuilder =
new DualKeyCacheBuilder<>();
dualKeyCache =
dualKeyCacheBuilder
.cacheEvictionPolicy(
DualKeyCachePolicy.valueOf(config.getDataNodeSchemaCacheEvictionPolicy()))
.memoryCapacity(config.getAllocateMemoryForSchemaCache())
.firstKeySizeComputer(TableId::estimateSize)
.secondKeySizeComputer(TableDeviceId::estimateSize)
.valueSizeComputer(TableDeviceCacheEntry::estimateSize)
.build();
}

public Map<String, String> getDeviceAttribute(
String database, String tableName, String[] deviceId) {
readWriteLock.readLock().lock();
try {
TableDeviceCacheEntry entry =
dualKeyCache.get(new TableId(database, tableName), new TableDeviceId(deviceId));
return entry == null ? null : entry.getAttributeMap();
} finally {
readWriteLock.readLock().unlock();
}
}

public void put(
String database, String tableName, String[] deviceId, Map<String, String> attributeMap) {
readWriteLock.readLock().lock();
try {
dualKeyCache.put(
new TableId(database, tableName),
new TableDeviceId(deviceId),
new TableDeviceCacheEntry(attributeMap));
} finally {
readWriteLock.readLock().unlock();
}
}

public void invalidate(String database, String tableName) {
readWriteLock.writeLock().lock();
try {
dualKeyCache.invalidateAll();
} finally {
readWriteLock.writeLock().unlock();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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.queryengine.plan.relational.metadata.fetcher.cache;

import org.apache.iotdb.commons.schema.MemUsageUtil;

import java.util.Objects;

public class TableId {

private final String database;

private final String tableName;

public TableId(String database, String tableName) {
this.database = database;
this.tableName = tableName;
}

public String getDatabase() {
return database;
}

public String getTableName() {
return tableName;
}

public int estimateSize() {
return 8
+ 8
+ 8
+ (int)
(MemUsageUtil.computeStringMemUsage(database)
+ MemUsageUtil.computeStringMemUsage(tableName));
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof TableId)) return false;
TableId tableId = (TableId) o;
return Objects.equals(database, tableId.database)
&& Objects.equals(tableName, tableId.tableName);
}

@Override
public int hashCode() {
return Objects.hash(database, tableName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* 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.queryengine.plan.relational.metadata.fetcher.cache;

import org.apache.iotdb.db.conf.IoTDBConfig;
import org.apache.iotdb.db.conf.IoTDBDescriptor;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

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

public class TableDeviceSchemaCacheTest {

private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();

private long originMemConfig;

@Before
public void setup() {
originMemConfig = config.getAllocateMemoryForSchemaCache();
config.setAllocateMemoryForSchemaCache(1500L);
}

@After
public void rollback() {
config.setAllocateMemoryForSchemaCache(originMemConfig);
}

@Test
public void testDeviceCache() {
TableDeviceSchemaCache cache = new TableDeviceSchemaCache();

String database = "db";
String table1 = "t1";

Map<String, String> attributeMap = new HashMap<>();
attributeMap.put("type", "new");
attributeMap.put("cycle", "monthly");
cache.put(database, table1, new String[] {"hebei", "p_1", "d_0"}, new HashMap<>(attributeMap));
Assert.assertEquals(
attributeMap,
cache.getDeviceAttribute(database, table1, new String[] {"hebei", "p_1", "d_0"}));
Assert.assertNull(
cache.getDeviceAttribute(database, table1, new String[] {"hebei", "p_1", "d_1"}));

attributeMap.put("type", "old");
cache.put(database, table1, new String[] {"hebei", "p_1", "d_1"}, new HashMap<>(attributeMap));
Assert.assertEquals(
attributeMap,
cache.getDeviceAttribute(database, table1, new String[] {"hebei", "p_1", "d_1"}));

attributeMap.put("cycle", "daily");
cache.put(
database, table1, new String[] {"shandong", "p_1", "d_1"}, new HashMap<>(attributeMap));
Assert.assertNull(
cache.getDeviceAttribute(database, table1, new String[] {"hebei", "p_1", "d_0"}));
Assert.assertEquals(
attributeMap,
cache.getDeviceAttribute(database, table1, new String[] {"shandong", "p_1", "d_1"}));

String table2 = "t1";
attributeMap.put("type", "new");
attributeMap.put("cycle", "monthly");
cache.put(database, table2, new String[] {"hebei", "p_1", "d_0"}, new HashMap<>(attributeMap));
Assert.assertEquals(
attributeMap,
cache.getDeviceAttribute(database, table2, new String[] {"hebei", "p_1", "d_0"}));
Assert.assertNull(
cache.getDeviceAttribute(database, table1, new String[] {"hebei", "p_1", "d_1"}));

attributeMap.put("type", "old");
cache.put(database, table2, new String[] {"hebei", "p_1", "d_1"}, new HashMap<>(attributeMap));
Assert.assertEquals(
attributeMap,
cache.getDeviceAttribute(database, table2, new String[] {"hebei", "p_1", "d_1"}));
Assert.assertNull(
cache.getDeviceAttribute(database, table1, new String[] {"shandong", "p_1", "d_1"}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ public static long computeKVMemUsageInMap(String key, String value) {
*/
private static long estimateStringSize(String string) {
// each char takes 2B in Java
return string == null ? 0 : 32 + 2 * string.length();
return string == null ? 0 : 32 + 2L * string.length();
}
}

0 comments on commit 532e743

Please sign in to comment.