-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[improve][fn] Implement pip 401: Support set batching configurations …
…for Pulsar Functions&Sources (#23860)
- Loading branch information
1 parent
114aaf0
commit fad925f
Showing
17 changed files
with
674 additions
and
11 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
pulsar-client-admin-api/src/main/java/org/apache/pulsar/common/functions/BatchingConfig.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,41 @@ | ||
/* | ||
* 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.pulsar.common.functions; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@EqualsAndHashCode | ||
public class BatchingConfig { | ||
@Builder.Default | ||
private boolean enabled = true; | ||
@Builder.Default | ||
private Integer batchingMaxPublishDelayMs = 10; | ||
private Integer roundRobinRouterBatchingPartitionSwitchFrequency; | ||
private Integer batchingMaxMessages; | ||
private Integer batchingMaxBytes; | ||
private String batchBuilder; | ||
} |
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
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
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
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
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
86 changes: 86 additions & 0 deletions
86
pulsar-functions/utils/src/main/java/org/apache/pulsar/functions/utils/BatchingUtils.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.pulsar.functions.utils; | ||
|
||
import org.apache.pulsar.common.functions.BatchingConfig; | ||
import org.apache.pulsar.functions.proto.Function; | ||
|
||
public final class BatchingUtils { | ||
public static Function.BatchingSpec convert(BatchingConfig config) { | ||
if (config == null) { | ||
return null; | ||
} | ||
|
||
Function.BatchingSpec.Builder builder = Function.BatchingSpec.newBuilder() | ||
.setEnabled(config.isEnabled()); | ||
|
||
if (config.getBatchingMaxPublishDelayMs() != null && config.getBatchingMaxPublishDelayMs() > 0) { | ||
builder.setBatchingMaxPublishDelayMs(config.getBatchingMaxPublishDelayMs()); | ||
} | ||
if (config.getRoundRobinRouterBatchingPartitionSwitchFrequency() != null | ||
&& config.getRoundRobinRouterBatchingPartitionSwitchFrequency() > 0) { | ||
builder.setRoundRobinRouterBatchingPartitionSwitchFrequency( | ||
config.getRoundRobinRouterBatchingPartitionSwitchFrequency()); | ||
} | ||
if (config.getBatchingMaxMessages() != null && config.getBatchingMaxMessages() > 0) { | ||
builder.setBatchingMaxMessages(config.getBatchingMaxMessages()); | ||
} | ||
if (config.getBatchingMaxBytes() != null && config.getBatchingMaxBytes() > 0) { | ||
builder.setBatchingMaxBytes(config.getBatchingMaxBytes()); | ||
} | ||
if (config.getBatchBuilder() != null && !config.getBatchBuilder().isEmpty()) { | ||
builder.setBatchBuilder(config.getBatchBuilder()); | ||
} | ||
|
||
return builder.build(); | ||
} | ||
|
||
public static BatchingConfig convertFromSpec(Function.BatchingSpec spec) { | ||
// to keep the backward compatibility, when batchingSpec is null or empty | ||
// the batching is enabled by default, and the default max publish delay is 10ms | ||
if (spec == null || spec.toString().equals("")) { | ||
return BatchingConfig.builder() | ||
.enabled(true) | ||
.batchingMaxPublishDelayMs(10) | ||
.build(); | ||
} | ||
|
||
BatchingConfig.BatchingConfigBuilder builder = BatchingConfig.builder() | ||
.enabled(spec.getEnabled()); | ||
|
||
if (spec.getBatchingMaxPublishDelayMs() > 0) { | ||
builder.batchingMaxPublishDelayMs(spec.getBatchingMaxPublishDelayMs()); | ||
} | ||
if (spec.getRoundRobinRouterBatchingPartitionSwitchFrequency() > 0) { | ||
builder.roundRobinRouterBatchingPartitionSwitchFrequency( | ||
spec.getRoundRobinRouterBatchingPartitionSwitchFrequency()); | ||
} | ||
if (spec.getBatchingMaxMessages() > 0) { | ||
builder.batchingMaxMessages(spec.getBatchingMaxMessages()); | ||
} | ||
if (spec.getBatchingMaxBytes() > 0) { | ||
builder.batchingMaxBytes(spec.getBatchingMaxBytes()); | ||
} | ||
if (spec.getBatchBuilder() != null && !spec.getBatchBuilder().isEmpty()) { | ||
builder.batchBuilder(spec.getBatchBuilder()); | ||
} | ||
|
||
return builder.build(); | ||
} | ||
} |
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
70 changes: 70 additions & 0 deletions
70
...ar-functions/utils/src/test/java/org/apache/pulsar/functions/utils/BatchingUtilsTest.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,70 @@ | ||
/* | ||
* 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.pulsar.functions.utils; | ||
|
||
import static org.testng.Assert.*; | ||
import org.apache.pulsar.common.functions.BatchingConfig; | ||
import org.apache.pulsar.functions.proto.Function; | ||
import org.testng.annotations.Test; | ||
|
||
public class BatchingUtilsTest { | ||
|
||
@Test | ||
public void testConvert() { | ||
BatchingConfig config = BatchingConfig.builder() | ||
.enabled(true) | ||
.batchingMaxPublishDelayMs(30) | ||
.roundRobinRouterBatchingPartitionSwitchFrequency(10) | ||
.batchingMaxMessages(1000) | ||
.batchBuilder("DEFAULT") | ||
.build(); | ||
Function.BatchingSpec spec = BatchingUtils.convert(config); | ||
assertEquals(spec.getEnabled(), true); | ||
assertEquals(spec.getBatchingMaxPublishDelayMs(), 30); | ||
assertEquals(spec.getRoundRobinRouterBatchingPartitionSwitchFrequency(), 10); | ||
assertEquals(spec.getBatchingMaxMessages(), 1000); | ||
assertEquals(spec.getBatchingMaxBytes(), 0); | ||
assertEquals(spec.getBatchBuilder(), "DEFAULT"); | ||
} | ||
|
||
@Test | ||
public void testConvertFromSpec() { | ||
Function.BatchingSpec spec = Function.BatchingSpec.newBuilder() | ||
.setEnabled(true) | ||
.setBatchingMaxPublishDelayMs(30) | ||
.setRoundRobinRouterBatchingPartitionSwitchFrequency(10) | ||
.setBatchingMaxMessages(1000) | ||
.setBatchBuilder("DEFAULT") | ||
.build(); | ||
BatchingConfig config = BatchingUtils.convertFromSpec(spec); | ||
assertEquals(config.isEnabled(), true); | ||
assertEquals(config.getBatchingMaxPublishDelayMs().intValue(), 30); | ||
assertEquals(config.getRoundRobinRouterBatchingPartitionSwitchFrequency().intValue(), 10); | ||
assertEquals(config.getBatchingMaxMessages().intValue(), 1000); | ||
assertEquals(config.getBatchingMaxBytes(), null); | ||
assertEquals(config.getBatchBuilder(), "DEFAULT"); | ||
} | ||
|
||
@Test | ||
public void testConvertFromSpecFromNull() { | ||
BatchingConfig config = BatchingUtils.convertFromSpec(null); | ||
assertTrue(config.isEnabled()); | ||
assertEquals(config.getBatchingMaxPublishDelayMs().intValue(), 10); | ||
} | ||
} |
Oops, something went wrong.