Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

LUCENE-8862: Introduce Collector Level Memory Accounting (#729)

Atri Sharma6799ca6efde8
diff --git a/lucene/misc/src/java/org/apache/lucene/misc/CollectorMemoryTracker.java b/lucene/misc/src/java/org/apache/lucene/misc/CollectorMemoryTracker.java
new file mode 100644
index 00000000000..544b760d00a
--- /dev/null
+++ b/lucene/misc/src/java/org/apache/lucene/misc/CollectorMemoryTracker.java
@@ -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.lucene.misc;
+
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.apache.lucene.util.MemoryTracker;
+
+/**
+ * Default implementation of {@code MemoryTracker} that tracks
+ * allocations and allows setting a memory limit per collector
+ */
+public class CollectorMemoryTracker implements MemoryTracker {
+ private String name;
+ private AtomicLong memoryUsage;
+ private final long memoryLimit;
+
+ public CollectorMemoryTracker(String name, long memoryLimit) {
+ this.name = name;
+ this.memoryUsage = new AtomicLong();
+ this.memoryLimit = memoryLimit;
+ }
+
+ @Override
+ public void updateBytes(long bytes) {
+ long currentMemoryUsage = memoryUsage.addAndGet(bytes);
+
+ if (currentMemoryUsage > memoryLimit) {
+ throw new IllegalStateException("Memory limit exceeded for " + name);
+ }
+ if (currentMemoryUsage < 0) {
+ throw new IllegalStateException("Illegal Memory State for " + name);
+ }
+ }
+
+ @Override
+ public long getBytes() {
+ return memoryUsage.get();
+ }
+}
diff --git a/lucene/misc/src/java/org/apache/lucene/search/MemoryAccountingBitsetCollector.java b/lucene/misc/src/java/org/apache/lucene/search/MemoryAccountingBitsetCollector.java
new file mode 100644
index 00000000000..953bfaaff44
--- /dev/null
+++ b/lucene/misc/src/java/org/apache/lucene/search/MemoryAccountingBitsetCollector.java
@@ -0,0 +1,58 @@
+/*
+ * 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.lucene.search;
+
+import java.io.IOException;
+
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.misc.CollectorMemoryTracker;
+import org.apache.lucene.util.FixedBitSet;
+
+/** Bitset collector which supports memory tracking */
+public class MemoryAccountingBitsetCollector extends SimpleCollector {
+
+ final CollectorMemoryTracker tracker;
+ FixedBitSet bitSet = new FixedBitSet(0);
+ int length = 0;
+ int docBase = 0;
+
+ public MemoryAccountingBitsetCollector(CollectorMemoryTracker tracker) {
+ this.tracker = tracker;
+ tracker.updateBytes(bitSet.ramBytesUsed());
+ }
+
+ @Override
+ protected void doSetNextReader(LeafReaderContext context) throws IOException {
+ length += context.reader().maxDoc();
+ FixedBitSet newBitSet = FixedBitSet.ensureCapacity(bitSet, length);
+ if (newBitSet != bitSet) {
+ tracker.updateBytes(newBitSet.ramBytesUsed() - bitSet.ramBytesUsed());
+ bitSet = newBitSet;
+ }
+ }
+
+ @Override
+ public void collect(int doc) {
+ bitSet.set(docBase + doc);
+ }
+
+ @Override
+ public ScoreMode scoreMode() {
+ return ScoreMode.COMPLETE_NO_SCORES;
+ }
+}
diff --git a/lucene/misc/src/java/org/apache/lucene/util/MemoryTracker.java b/lucene/misc/src/java/org/apache/lucene/util/MemoryTracker.java
new file mode 100644
index 00000000000..af94cede975
--- /dev/null
+++ b/lucene/misc/src/java/org/apache/lucene/util/MemoryTracker.java
@@ -0,0 +1,26 @@
+/*
+ * 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.lucene.util;
+
+/**
+ * Tracks dynamic allocations/deallocations of memory for transient objects
+ */
+public interface MemoryTracker {
+ void updateBytes(long bytes);
+ long getBytes();
+}
diff --git a/lucene/misc/src/java/org/apache/lucene/util/package.html b/lucene/misc/src/java/org/apache/lucene/util/package.html
new file mode 100644
index 00000000000..24aa5071ae6
--- /dev/null
+++ b/lucene/misc/src/java/org/apache/lucene/util/package.html
@@ -0,0 +1,23 @@
+<!--
+ 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.
+-->
+<!-- not a package-info.java, because we already defined this package in core/ -->
+<html>
+<body>
+Memory Tracker interface which allows defining custom collector
+level memory trackers
+</body>
+</html>
diff --git a/lucene/misc/src/test/org/apache/lucene/search/TestMemoryAccountingBitsetCollector.java b/lucene/misc/src/test/org/apache/lucene/search/TestMemoryAccountingBitsetCollector.java
new file mode 100644
index 00000000000..fdd163c4011
--- /dev/null
+++ b/lucene/misc/src/test/org/apache/lucene/search/TestMemoryAccountingBitsetCollector.java
@@ -0,0 +1,69 @@
+/*
+ * 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.lucene.search;
+
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+import org.apache.lucene.document.SortedDocValuesField;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.RandomIndexWriter;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.BytesRef;
+import org.apache.lucene.misc.CollectorMemoryTracker;
+import org.apache.lucene.util.LuceneTestCase;
+
+public class TestMemoryAccountingBitsetCollector extends LuceneTestCase {
+
+ Directory dir;
+ IndexReader reader;
+
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ dir = newDirectory();
+ RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
+ for (int i = 0; i < 1000; i++) {
+ Document doc = new Document();
+ doc.add(newStringField("field", Integer.toString(i), Field.Store.NO));
+ doc.add(newStringField("field2", Boolean.toString(i % 2 == 0), Field.Store.NO));
+ doc.add(new SortedDocValuesField("field2", new BytesRef(Boolean.toString(i % 2 == 0))));
+ iw.addDocument(doc);
+ }
+ reader = iw.getReader();
+ iw.close();
+ }
+
+ @Override
+ public void tearDown() throws Exception {
+ super.tearDown();
+ reader.close();
+ dir.close();
+ }
+
+ public void testMemoryAccountingBitsetCollectorMemoryLimit() {
+ long perCollectorMemoryLimit = 150;
+ CollectorMemoryTracker tracker = new CollectorMemoryTracker("testMemoryTracker", perCollectorMemoryLimit);
+ MemoryAccountingBitsetCollector bitSetCollector = new MemoryAccountingBitsetCollector(tracker);
+ TotalHitCountCollector hitCountCollector = new TotalHitCountCollector();
+
+ IndexSearcher searcher = new IndexSearcher(reader);
+ expectThrows(IllegalStateException.class, () -> {
+ searcher.search(new MatchAllDocsQuery(), MultiCollector.wrap(hitCountCollector, bitSetCollector));
+ });
+ }
+}
diff --git a/lucene/misc/src/test/org/apache/lucene/util/TestCollectorMemoryTracker.java b/lucene/misc/src/test/org/apache/lucene/util/TestCollectorMemoryTracker.java
new file mode 100644
index 00000000000..aaa5f6dd233
--- /dev/null
+++ b/lucene/misc/src/test/org/apache/lucene/util/TestCollectorMemoryTracker.java
@@ -0,0 +1,42 @@
+/*
+ * 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.lucene.util;
+
+import org.apache.lucene.misc.CollectorMemoryTracker;
+
+public class TestCollectorMemoryTracker extends LuceneTestCase {
+ public void testAdditionsAndDeletions() {
+ long perCollectorMemoryLimit = 100; //100 Bytes
+ CollectorMemoryTracker collectorMemoryTracker = new CollectorMemoryTracker("testMemoryTracker",
+ perCollectorMemoryLimit);
+
+ collectorMemoryTracker.updateBytes(50);
+ assertEquals(collectorMemoryTracker.getBytes(), 50);
+ collectorMemoryTracker.updateBytes(-30);
+ assertEquals(collectorMemoryTracker.getBytes(), 20);
+ expectThrows(IllegalStateException.class, () -> {
+ collectorMemoryTracker.updateBytes(130);
+ });
+ collectorMemoryTracker.updateBytes(-110);
+ assertEquals(collectorMemoryTracker.getBytes(), 40);
+
+ expectThrows(IllegalStateException.class, () -> {
+ collectorMemoryTracker.updateBytes(-90);
+ });
+ }
+}