-
Notifications
You must be signed in to change notification settings - Fork 974
Add memory limiter for the add entry request to avoid OOM #3139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
a04b741
a55b91b
8d747ee
19e3d5c
33f8fbb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /** | ||
| * 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 | ||
| * <p> | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * <p> | ||
| * 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.bookkeeper.common.util; | ||
|
|
||
| /** | ||
| * WritableListener used to listen the writable status changes. | ||
| * | ||
| * We use {@link WriteMemoryCounter} to listen on the memory usage when the client adds entries. The listener can | ||
| * take actions if they have been notified. | ||
| */ | ||
| public interface WritableListener { | ||
|
|
||
| void onWriteStateChanged(boolean writable); | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /** | ||
| * 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.bookkeeper.common.util; | ||
|
|
||
| import java.util.LinkedList; | ||
| import java.util.List; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| /** | ||
| * {@link WriteMemoryCounter} counts the memory usage on Adds request. | ||
| * When there has an Add request created, the {@link WriteMemoryCounter} will record the request content size. | ||
| * When the request is finished, the {@link WriteMemoryCounter} will decrease the record count. | ||
| * The range of the counter should in the range of {@link WriteWaterMark}'s high watermark and low watermark. | ||
| * | ||
| * If the record size is over to the high watermark, the registered listeners will receive writable state change | ||
| * to false and take actions. The listeners will receive writable state change to true until the record size is | ||
| * down to the low watermark. | ||
| */ | ||
| @Slf4j | ||
| public class WriteMemoryCounter { | ||
| private final WriteWaterMark writeWaterMark; | ||
| private AtomicLong sizeCounter = new AtomicLong(0); | ||
| private AtomicBoolean writeState = new AtomicBoolean(true); | ||
| private final List<WritableListener> listeners = new LinkedList<>(); | ||
|
|
||
| public WriteMemoryCounter(WriteWaterMark writeWaterMark) { | ||
| this.writeWaterMark = writeWaterMark; | ||
| } | ||
|
|
||
| public WriteMemoryCounter() { | ||
| this.writeWaterMark = new WriteWaterMark(); | ||
| } | ||
|
|
||
| public void register(WritableListener listener) { | ||
| listeners.add(listener); | ||
| } | ||
|
|
||
| public void incrementPendingWriteBytes(long size) { | ||
| long usage = sizeCounter.addAndGet(size); | ||
| log.info("increment the size to {}", usage); | ||
| if (usage > writeWaterMark.high() && writeState.get()) { | ||
| setWritable(false); | ||
| } | ||
| } | ||
|
|
||
| public void decrementPendingWriteBytes(long size) { | ||
| long usage = sizeCounter.addAndGet(-size); | ||
| log.info("decrement the size to {}", usage); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. change to debug level? |
||
| if (usage < writeWaterMark.low() && !writeState.get()) { | ||
| setWritable(true); | ||
| } | ||
| } | ||
|
|
||
| public void setWritable(boolean state) { | ||
| writeState.set(state); | ||
| listeners.forEach(l -> l.onWriteStateChanged(state)); | ||
| } | ||
|
|
||
| public long getSize() { | ||
| return sizeCounter.get(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /** | ||
| * 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.bookkeeper.common.util; | ||
|
|
||
| /** | ||
| * {@link WriteWaterMark} is used to configure the max value and the min value of the memory usage. | ||
| */ | ||
| public class WriteWaterMark { | ||
| private static final long DEFAULT_LOW_WATER_MARK = 1610612736L; // 1.5GB | ||
| private static final long DEFAULT_HIGH_WATER_MARK = 2147483648L; // 2GB | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the default |
||
|
|
||
| private final long low; | ||
| private final long high; | ||
|
|
||
| public WriteWaterMark(long low, long high) { | ||
| this.low = low; | ||
| this.high = high; | ||
| } | ||
|
|
||
| public WriteWaterMark() { | ||
| this.low = DEFAULT_LOW_WATER_MARK; | ||
| this.high = DEFAULT_HIGH_WATER_MARK; | ||
| } | ||
|
|
||
| public long low() { | ||
| return low; | ||
| } | ||
|
|
||
| public long high() { | ||
| return high; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -199,6 +199,9 @@ public class ClientConfiguration extends AbstractConfiguration<ClientConfigurati | |
| protected static final String CLIENT_CONNECT_BOOKIE_UNAVAILABLE_LOG_THROTTLING = | ||
| "clientConnectBookieUnavailableLogThrottling"; | ||
|
|
||
| protected static final String WRITE_MEMORY_LOW_WATER_MARK = "writeMemoryLowWaterMark"; | ||
| protected static final String WRITE_MEMORY_HIGH_WATER_MARK = "writeMemoryHighWaterMark"; | ||
|
|
||
| /** | ||
| * Construct a default client-side configuration. | ||
| */ | ||
|
|
@@ -2058,6 +2061,24 @@ public long getClientConnectBookieUnavailableLogThrottlingMs() { | |
| return getLong(CLIENT_CONNECT_BOOKIE_UNAVAILABLE_LOG_THROTTLING, 5_000L); | ||
| } | ||
|
|
||
| public ClientConfiguration setWriteMemoryLowWaterMark(long bytes) { | ||
| setProperty(WRITE_MEMORY_LOW_WATER_MARK, bytes); | ||
| return this; | ||
| } | ||
|
|
||
| public long getWriteMemoryLowWaterMark() { | ||
| return getInt(WRITE_MEMORY_LOW_WATER_MARK, 64 * 1024 * 1024); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. getLong? |
||
| } | ||
|
|
||
| public ClientConfiguration setWriteMemoryHighWaterMark(long bytes) { | ||
| setProperty(WRITE_MEMORY_HIGH_WATER_MARK, bytes); | ||
| return this; | ||
| } | ||
|
|
||
| public long getWriteMemoryHighWaterMark() { | ||
| return getInt(WRITE_MEMORY_HIGH_WATER_MARK, 256 * 1024 * 1024); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. getLong? |
||
| } | ||
|
|
||
| @Override | ||
| protected ClientConfiguration getThis() { | ||
| return this; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
final type?