-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Table Model] Implement table device cache (#12702)
- Loading branch information
Showing
6 changed files
with
371 additions
and
1 deletion.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
...he/iotdb/db/queryengine/plan/relational/metadata/fetcher/cache/TableDeviceCacheEntry.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,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; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...org/apache/iotdb/db/queryengine/plan/relational/metadata/fetcher/cache/TableDeviceId.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,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); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
...e/iotdb/db/queryengine/plan/relational/metadata/fetcher/cache/TableDeviceSchemaCache.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,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(); | ||
} | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
.../java/org/apache/iotdb/db/queryengine/plan/relational/metadata/fetcher/cache/TableId.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,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); | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
...tdb/db/queryengine/plan/relational/metadata/fetcher/cache/TableDeviceSchemaCacheTest.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,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"})); | ||
} | ||
} |
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