You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@lucene.apache.org by "magibney (via GitHub)" <gi...@apache.org> on 2024/07/09 16:03:14 UTC
[PR] Group memory arenas by segment to reduce costly `Arena.close()` [lucene]
magibney opened a new pull request, #13555:
URL: https://github.com/apache/lucene/pull/13555
(no comment)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Group memory arenas by segment to reduce costly `Arena.close()` [lucene]
Posted by "magibney (via GitHub)" <gi...@apache.org>.
magibney commented on code in PR #13555:
URL: https://github.com/apache/lucene/pull/13555#discussion_r1674096237
##########
lucene/core/src/java21/org/apache/lucene/store/GroupedArena.java:
##########
@@ -0,0 +1,212 @@
+/*
+ * 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.store;
+
+import java.lang.foreign.AddressLayout;
+import java.lang.foreign.Arena;
+import java.lang.foreign.MemoryLayout;
+import java.lang.foreign.MemorySegment;
+import java.lang.foreign.ValueLayout;
+import java.nio.file.Path;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.lucene.index.IndexFileNames;
+
+@SuppressWarnings("preview")
+final class GroupedArena implements Arena {
+
+ private final String scopeId;
+
+ private final ConcurrentHashMap<String, GroupedArena> arenas;
+
+ private final Arena backing;
+
+ private final AtomicInteger refCt;
+
+ static Arena get(Path p, ConcurrentHashMap<String, GroupedArena> arenas) {
+ String filename = p.getFileName().toString();
+ String segmentName = IndexFileNames.parseSegmentName(filename);
+ if (filename.length() == segmentName.length()) {
+ // no segment found; return a 1-off Arena
+ return Arena.ofShared();
+ }
+ String scopeId = p.getParent().resolve(segmentName).toString();
+ Arena ret;
+ do {
+ boolean[] computed = new boolean[1];
+ final GroupedArena template =
+ arenas.computeIfAbsent(
+ scopeId,
+ (s) -> {
+ computed[0] = true;
+ return new GroupedArena(s, arenas);
+ });
+ if (computed[0]) {
+ return template;
+ }
+ ret = template.cloneIfActive();
+ } while (ret == null); // TODO: will this ever actually loop?
+ return ret;
+ }
+
+ GroupedArena(String scopeId, ConcurrentHashMap<String, GroupedArena> arenas) {
+ this.scopeId = scopeId;
+ this.arenas = arenas;
+ this.backing = Arena.ofShared();
+ this.refCt = new AtomicInteger(1);
+ }
+
+ private GroupedArena(GroupedArena template) {
+ this.scopeId = template.scopeId;
+ this.arenas = template.arenas;
+ this.backing = template.backing;
+ this.refCt = template.refCt;
+ }
+
+ private GroupedArena cloneIfActive() {
+ if (refCt.getAndIncrement() > 0) {
+ // the usual (always?) case
+ return new GroupedArena(this);
+ } else {
+ // TODO: this should never happen?
+ return null;
+ }
+ }
+
+ @Override
+ public void close() {
+ int ct = refCt.decrementAndGet();
+ if (ct == 0) {
+ arenas.remove(scopeId);
+ if (refCt.get() == 0) {
+ // TODO: this should always be the case? But if it's not, it should be a benign
+ // race condition. Whatever caller incremented `refCt` will close it, and if
+ // anyone tries to open a new arena with the same `scopeId` that we removed
+ // above, they'll simply create a new Arena, and we're no worse off than we
+ // would have been if every Arena was created as a one-off.
+ backing.close();
+ }
+ } else {
+ assert ct > 0 : "refCt should never be negative; found " + ct;
+ }
+ }
+
+ @Override
Review Comment:
Ah right; I hadn't bothered looking at how Arena is actually used. Should we not throw UOE for _all_ of them? All of these have default implementations, so we end up only having to override `scope()` and `close()`, which are the only required to implement (and also the only ones we actually call).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Group memory arenas by segment to reduce costly `Arena.close()` [lucene]
Posted by "ChrisHegarty (via GitHub)" <gi...@apache.org>.
ChrisHegarty commented on code in PR #13555:
URL: https://github.com/apache/lucene/pull/13555#discussion_r1673543647
##########
lucene/core/src/java21/org/apache/lucene/store/GroupedArena.java:
##########
@@ -0,0 +1,212 @@
+/*
+ * 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.store;
+
+import java.lang.foreign.AddressLayout;
+import java.lang.foreign.Arena;
+import java.lang.foreign.MemoryLayout;
+import java.lang.foreign.MemorySegment;
+import java.lang.foreign.ValueLayout;
+import java.nio.file.Path;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.lucene.index.IndexFileNames;
+
+@SuppressWarnings("preview")
+final class GroupedArena implements Arena {
+
+ private final String scopeId;
+
+ private final ConcurrentHashMap<String, GroupedArena> arenas;
+
+ private final Arena backing;
+
+ private final AtomicInteger refCt;
+
+ static Arena get(Path p, ConcurrentHashMap<String, GroupedArena> arenas) {
+ String filename = p.getFileName().toString();
+ String segmentName = IndexFileNames.parseSegmentName(filename);
+ if (filename.length() == segmentName.length()) {
+ // no segment found; return a 1-off Arena
+ return Arena.ofShared();
+ }
+ String scopeId = p.getParent().resolve(segmentName).toString();
+ Arena ret;
+ do {
+ boolean[] computed = new boolean[1];
+ final GroupedArena template =
+ arenas.computeIfAbsent(
+ scopeId,
+ (s) -> {
+ computed[0] = true;
+ return new GroupedArena(s, arenas);
+ });
+ if (computed[0]) {
+ return template;
+ }
+ ret = template.cloneIfActive();
+ } while (ret == null); // TODO: will this ever actually loop?
+ return ret;
+ }
+
+ GroupedArena(String scopeId, ConcurrentHashMap<String, GroupedArena> arenas) {
+ this.scopeId = scopeId;
+ this.arenas = arenas;
+ this.backing = Arena.ofShared();
+ this.refCt = new AtomicInteger(1);
+ }
+
+ private GroupedArena(GroupedArena template) {
+ this.scopeId = template.scopeId;
+ this.arenas = template.arenas;
+ this.backing = template.backing;
+ this.refCt = template.refCt;
+ }
+
+ private GroupedArena cloneIfActive() {
+ if (refCt.getAndIncrement() > 0) {
+ // the usual (always?) case
+ return new GroupedArena(this);
+ } else {
+ // TODO: this should never happen?
+ return null;
+ }
+ }
+
+ @Override
+ public void close() {
+ int ct = refCt.decrementAndGet();
+ if (ct == 0) {
+ arenas.remove(scopeId);
+ if (refCt.get() == 0) {
+ // TODO: this should always be the case? But if it's not, it should be a benign
+ // race condition. Whatever caller incremented `refCt` will close it, and if
+ // anyone tries to open a new arena with the same `scopeId` that we removed
+ // above, they'll simply create a new Arena, and we're no worse off than we
+ // would have been if every Arena was created as a one-off.
+ backing.close();
+ }
+ } else {
+ assert ct > 0 : "refCt should never be negative; found " + ct;
+ }
+ }
+
+ @Override
Review Comment:
++
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Group memory arenas by segment to reduce costly `Arena.close()` [lucene]
Posted by "uschindler (via GitHub)" <gi...@apache.org>.
uschindler commented on code in PR #13555:
URL: https://github.com/apache/lucene/pull/13555#discussion_r1673613242
##########
lucene/core/src/java21/org/apache/lucene/store/MemorySegmentIndexInputProvider.java:
##########
@@ -24,12 +24,15 @@
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
import org.apache.lucene.util.Constants;
import org.apache.lucene.util.Unwrappable;
@SuppressWarnings("preview")
final class MemorySegmentIndexInputProvider implements MMapDirectory.MMapIndexInputProvider {
+ private static final ConcurrentHashMap<String, GroupedArena> ARENAS = new ConcurrentHashMap<>();
Review Comment:
Maybe we can make it `CHM<String,AutoCloseable>`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Group memory arenas by segment to reduce costly `Arena.close()` [lucene]
Posted by "uschindler (via GitHub)" <gi...@apache.org>.
uschindler commented on code in PR #13555:
URL: https://github.com/apache/lucene/pull/13555#discussion_r1672768505
##########
lucene/core/src/java21/org/apache/lucene/store/GroupedArena.java:
##########
@@ -0,0 +1,212 @@
+/*
+ * 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.store;
+
+import java.lang.foreign.AddressLayout;
+import java.lang.foreign.Arena;
+import java.lang.foreign.MemoryLayout;
+import java.lang.foreign.MemorySegment;
+import java.lang.foreign.ValueLayout;
+import java.nio.file.Path;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.lucene.index.IndexFileNames;
+
+@SuppressWarnings("preview")
+final class GroupedArena implements Arena {
+
+ private final String scopeId;
+
+ private final ConcurrentHashMap<String, GroupedArena> arenas;
+
+ private final Arena backing;
+
+ private final AtomicInteger refCt;
+
+ static Arena get(Path p, ConcurrentHashMap<String, GroupedArena> arenas) {
+ String filename = p.getFileName().toString();
+ String segmentName = IndexFileNames.parseSegmentName(filename);
+ if (filename.length() == segmentName.length()) {
+ // no segment found; return a 1-off Arena
+ return Arena.ofShared();
+ }
+ String scopeId = p.getParent().resolve(segmentName).toString();
+ Arena ret;
+ do {
+ boolean[] computed = new boolean[1];
+ final GroupedArena template =
+ arenas.computeIfAbsent(
+ scopeId,
+ (s) -> {
+ computed[0] = true;
+ return new GroupedArena(s, arenas);
+ });
+ if (computed[0]) {
+ return template;
+ }
+ ret = template.cloneIfActive();
+ } while (ret == null); // TODO: will this ever actually loop?
+ return ret;
+ }
+
+ GroupedArena(String scopeId, ConcurrentHashMap<String, GroupedArena> arenas) {
+ this.scopeId = scopeId;
+ this.arenas = arenas;
+ this.backing = Arena.ofShared();
+ this.refCt = new AtomicInteger(1);
+ }
+
+ private GroupedArena(GroupedArena template) {
+ this.scopeId = template.scopeId;
+ this.arenas = template.arenas;
+ this.backing = template.backing;
+ this.refCt = template.refCt;
+ }
+
+ private GroupedArena cloneIfActive() {
+ if (refCt.getAndIncrement() > 0) {
+ // the usual (always?) case
+ return new GroupedArena(this);
+ } else {
+ // TODO: this should never happen?
+ return null;
+ }
+ }
+
+ @Override
+ public void close() {
+ int ct = refCt.decrementAndGet();
+ if (ct == 0) {
+ arenas.remove(scopeId);
+ if (refCt.get() == 0) {
+ // TODO: this should always be the case? But if it's not, it should be a benign
+ // race condition. Whatever caller incremented `refCt` will close it, and if
+ // anyone tries to open a new arena with the same `scopeId` that we removed
+ // above, they'll simply create a new Arena, and we're no worse off than we
+ // would have been if every Arena was created as a one-off.
+ backing.close();
+ }
+ } else {
+ assert ct > 0 : "refCt should never be negative; found " + ct;
+ }
+ }
+
+ @Override
Review Comment:
I am not so happy that we need to implement all those methods. Let's keep the default ones.
Maybe let the required ones throw UOE, because we never use the arena to allocate memory.
##########
lucene/core/src/java21/org/apache/lucene/store/GroupedArena.java:
##########
@@ -0,0 +1,212 @@
+/*
+ * 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.store;
+
+import java.lang.foreign.AddressLayout;
+import java.lang.foreign.Arena;
+import java.lang.foreign.MemoryLayout;
+import java.lang.foreign.MemorySegment;
+import java.lang.foreign.ValueLayout;
+import java.nio.file.Path;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.lucene.index.IndexFileNames;
+
+@SuppressWarnings("preview")
+final class GroupedArena implements Arena {
+
+ private final String scopeId;
+
+ private final ConcurrentHashMap<String, GroupedArena> arenas;
+
+ private final Arena backing;
+
+ private final AtomicInteger refCt;
+
+ static Arena get(Path p, ConcurrentHashMap<String, GroupedArena> arenas) {
+ String filename = p.getFileName().toString();
+ String segmentName = IndexFileNames.parseSegmentName(filename);
+ if (filename.length() == segmentName.length()) {
+ // no segment found; return a 1-off Arena
+ return Arena.ofShared();
+ }
+ String scopeId = p.getParent().resolve(segmentName).toString();
+ Arena ret;
+ do {
Review Comment:
this code is a bit strange, also with the boolean[] as modifiable holder. I'd rewrite this.
Basically the code should better check the refcount of the GroupedArena and if it is 0 return itself, incrementing by 1. For all other cases it shoiuld just return a clone with incremented refcount.
Ret should never be 0, please remove the code that could make it null in the coloneIfActive(). While loop shouldn't be needed.
##########
lucene/core/src/java21/org/apache/lucene/store/MemorySegmentIndexInputProvider.java:
##########
@@ -24,12 +24,15 @@
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
import org.apache.lucene.util.Constants;
import org.apache.lucene.util.Unwrappable;
@SuppressWarnings("preview")
final class MemorySegmentIndexInputProvider implements MMapDirectory.MMapIndexInputProvider {
+ private static final ConcurrentHashMap<String, GroupedArena> ARENAS = new ConcurrentHashMap<>();
Review Comment:
I don't like that it is a static global arena. It should be private per MMapDirectory instance. This should be doable, as we can pass the map to `GroupedArena.get(path, arena)`. So it can also be a private per-instance one.
If you do this, the file name is the only key needed. The current code with adding full path is only needed because it is global.
The ConcurrentHashMap expeczted should be typed `ConcurrentHashMap<String, ?>` as `Arena` should never escape the private Java 21 code.
I am afraid of memory leaks caused by this.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Group memory arenas by segment to reduce costly `Arena.close()` [lucene]
Posted by "magibney (via GitHub)" <gi...@apache.org>.
magibney commented on PR #13555:
URL: https://github.com/apache/lucene/pull/13555#issuecomment-2228844256
Closing in favor of #13570
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Group memory arenas by segment to reduce costly `Arena.close()` [lucene]
Posted by "ChrisHegarty (via GitHub)" <gi...@apache.org>.
ChrisHegarty commented on PR #13555:
URL: https://github.com/apache/lucene/pull/13555#issuecomment-2227465099
Hi, I don't have access to commit to this branch, so (sorry) I just created an alternative PR to sketch out an idea around a simplification of ref counting the arena. See #13570
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Group memory arenas by segment to reduce costly `Arena.close()` [lucene]
Posted by "uschindler (via GitHub)" <gi...@apache.org>.
uschindler commented on code in PR #13555:
URL: https://github.com/apache/lucene/pull/13555#discussion_r1673613242
##########
lucene/core/src/java21/org/apache/lucene/store/MemorySegmentIndexInputProvider.java:
##########
@@ -24,12 +24,15 @@
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
import org.apache.lucene.util.Constants;
import org.apache.lucene.util.Unwrappable;
@SuppressWarnings("preview")
final class MemorySegmentIndexInputProvider implements MMapDirectory.MMapIndexInputProvider {
+ private static final ConcurrentHashMap<String, GroupedArena> ARENAS = new ConcurrentHashMap<>();
Review Comment:
Maybe we can make it `CHM<String,Closeable>`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Group memory arenas by segment to reduce costly `Arena.close()` [lucene]
Posted by "magibney (via GitHub)" <gi...@apache.org>.
magibney closed pull request #13555: Group memory arenas by segment to reduce costly `Arena.close()`
URL: https://github.com/apache/lucene/pull/13555
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Group memory arenas by segment to reduce costly `Arena.close()` [lucene]
Posted by "magibney (via GitHub)" <gi...@apache.org>.
magibney commented on PR #13555:
URL: https://github.com/apache/lucene/pull/13555#issuecomment-2218482093
Follows an approach analogous to the ["custom arenas"](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/foreign/Arena.html#custom-arenas) case described in Arena javadocs.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Group memory arenas by segment to reduce costly `Arena.close()` [lucene]
Posted by "magibney (via GitHub)" <gi...@apache.org>.
magibney commented on code in PR #13555:
URL: https://github.com/apache/lucene/pull/13555#discussion_r1674172898
##########
lucene/core/src/java21/org/apache/lucene/store/GroupedArena.java:
##########
@@ -0,0 +1,212 @@
+/*
+ * 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.store;
+
+import java.lang.foreign.AddressLayout;
+import java.lang.foreign.Arena;
+import java.lang.foreign.MemoryLayout;
+import java.lang.foreign.MemorySegment;
+import java.lang.foreign.ValueLayout;
+import java.nio.file.Path;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.lucene.index.IndexFileNames;
+
+@SuppressWarnings("preview")
+final class GroupedArena implements Arena {
+
+ private final String scopeId;
+
+ private final ConcurrentHashMap<String, GroupedArena> arenas;
+
+ private final Arena backing;
+
+ private final AtomicInteger refCt;
+
+ static Arena get(Path p, ConcurrentHashMap<String, GroupedArena> arenas) {
+ String filename = p.getFileName().toString();
+ String segmentName = IndexFileNames.parseSegmentName(filename);
+ if (filename.length() == segmentName.length()) {
+ // no segment found; return a 1-off Arena
+ return Arena.ofShared();
+ }
+ String scopeId = p.getParent().resolve(segmentName).toString();
+ Arena ret;
+ do {
Review Comment:
60ff62d9cdd9dec584b8e6ce63e4a7af13b828a2 rewrites this (without the need to clone, and without the `boolean[]` modifiable holder), but I still think we may need the while loop; I tried to better explain my reasoning in the comment:
```java
// We loop below to protect against the possibility that we get an entry that is
// in the process of being closed. It is unlikely (perhaps impossible?) that this
// would happen in practice, based on how Lucene opens IndexInputs. But if we
// don't check here it's theoretically possible that our return value would be
// backed by an Arena that is closed (and would throw IllegalStateException).
// NOTE also: if we admit the possibility that a `Directory` can be used for
// purposes other than a Lucene index, then we don't have the implicit protection
// conferred by expected usage patterns, and it becomes all the more important to
// loop to protect against the possibility of concurrent open/close.
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Group memory arenas by segment to reduce costly `Arena.close()` [lucene]
Posted by "magibney (via GitHub)" <gi...@apache.org>.
magibney commented on PR #13555:
URL: https://github.com/apache/lucene/pull/13555#issuecomment-2223189418
Thanks for the suggestions! Most of these are now addressed (with a couple of questions: 1. maybe still need chm.computeIfAbsent() loop, 2. which if any Arena methods to throw UOE).
I also wanted to call attention to another concern that occurred to me, described in a comment:
```java
// TODO: what if the associated Directory is used for some purpose other than Lucene
// index files, and some of the filenames happen to be patterned so that a "segment"
// can be parsed from them? That could result in files being spuriously grouped
// together in the same arena. This would only practically be a problem if such a
// "segment" was never closed (refCt never 0), _and_ enough files associated with that
// "segment" were opened and closed (without ever closing the associated Arena)
// to exhaust virtual memory space.
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Group memory arenas by segment to reduce costly `Arena.close()` [lucene]
Posted by "ChrisHegarty (via GitHub)" <gi...@apache.org>.
ChrisHegarty commented on code in PR #13555:
URL: https://github.com/apache/lucene/pull/13555#discussion_r1673550548
##########
lucene/core/src/java21/org/apache/lucene/store/MemorySegmentIndexInputProvider.java:
##########
@@ -24,12 +24,15 @@
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
import org.apache.lucene.util.Constants;
import org.apache.lucene.util.Unwrappable;
@SuppressWarnings("preview")
final class MemorySegmentIndexInputProvider implements MMapDirectory.MMapIndexInputProvider {
+ private static final ConcurrentHashMap<String, GroupedArena> ARENAS = new ConcurrentHashMap<>();
Review Comment:
yes, let's make this an instance field on MMapDirectory.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org