forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate out FunctionMetadata related helper functions (apache#7146)
* Seperate out FunctionMetaData related functions into a utility class * Fixed bug Co-authored-by: Sanjeev Kulkarni <[email protected]>
- Loading branch information
Showing
5 changed files
with
193 additions
and
66 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
...unctions/utils/src/main/java/org/apache/pulsar/functions/utils/FunctionMetaDataUtils.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,79 @@ | ||
/** | ||
* 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.functions.proto.Function; | ||
|
||
public class FunctionMetaDataUtils { | ||
|
||
public static boolean canChangeState(Function.FunctionMetaData functionMetaData, int instanceId, Function.FunctionState newState) { | ||
if (instanceId >= functionMetaData.getFunctionDetails().getParallelism()) { | ||
return false; | ||
} | ||
if (functionMetaData.getInstanceStatesMap() == null || functionMetaData.getInstanceStatesMap().isEmpty()) { | ||
// This means that all instances of the functions are running | ||
return newState == Function.FunctionState.STOPPED; | ||
} | ||
if (instanceId >= 0) { | ||
if (functionMetaData.getInstanceStatesMap().containsKey(instanceId)) { | ||
return functionMetaData.getInstanceStatesMap().get(instanceId) != newState; | ||
} else { | ||
return false; | ||
} | ||
} else { | ||
// want to change state for all instances | ||
for (Function.FunctionState state : functionMetaData.getInstanceStatesMap().values()) { | ||
if (state != newState) return true; | ||
} | ||
return false; | ||
} | ||
} | ||
|
||
public static Function.FunctionMetaData changeFunctionInstanceStatus(Function.FunctionMetaData functionMetaData, | ||
Integer instanceId, boolean start) { | ||
Function.FunctionMetaData.Builder builder = functionMetaData.toBuilder() | ||
.setVersion(functionMetaData.getVersion() + 1); | ||
if (builder.getInstanceStatesMap() == null || builder.getInstanceStatesMap().isEmpty()) { | ||
for (int i = 0; i < functionMetaData.getFunctionDetails().getParallelism(); ++i) { | ||
builder.putInstanceStates(i, Function.FunctionState.RUNNING); | ||
} | ||
} | ||
Function.FunctionState state = start ? Function.FunctionState.RUNNING : Function.FunctionState.STOPPED; | ||
if (instanceId < 0) { | ||
for (int i = 0; i < functionMetaData.getFunctionDetails().getParallelism(); ++i) { | ||
builder.putInstanceStates(i, state); | ||
} | ||
} else if (instanceId < builder.getFunctionDetails().getParallelism()){ | ||
builder.putInstanceStates(instanceId, state); | ||
} | ||
return builder.build(); | ||
} | ||
|
||
public static Function.FunctionMetaData generateUpdatedMetadata(Function.FunctionMetaData existingMetaData, | ||
Function.FunctionMetaData updatedMetaData) { | ||
long version = 0; | ||
if (existingMetaData != null) { | ||
version = existingMetaData.getVersion() + 1; | ||
} | ||
return updatedMetaData.toBuilder() | ||
.setVersion(version) | ||
.build(); | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
...ions/utils/src/test/java/org/apache/pulsar/functions/utils/FunctionMetaDataUtilsTest.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,95 @@ | ||
/** | ||
* 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.functions.proto.Function; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
/** | ||
* Unit test of {@link FunctionMetaDataUtils}. | ||
*/ | ||
public class FunctionMetaDataUtilsTest { | ||
|
||
@Test | ||
public void testCanChangeState() { | ||
|
||
long version = 5; | ||
Function.FunctionMetaData metaData = Function.FunctionMetaData.newBuilder().setFunctionDetails( | ||
Function.FunctionDetails.newBuilder().setName("func-1").setParallelism(2)).setVersion(version).build(); | ||
|
||
Assert.assertTrue(FunctionMetaDataUtils.canChangeState(metaData, 0, Function.FunctionState.STOPPED)); | ||
Assert.assertFalse(FunctionMetaDataUtils.canChangeState(metaData, 0, Function.FunctionState.RUNNING)); | ||
Assert.assertFalse(FunctionMetaDataUtils.canChangeState(metaData, 2, Function.FunctionState.STOPPED)); | ||
Assert.assertFalse(FunctionMetaDataUtils.canChangeState(metaData, 2, Function.FunctionState.RUNNING)); | ||
} | ||
|
||
@Test | ||
public void testChangeState() { | ||
long version = 5; | ||
Function.FunctionMetaData metaData = Function.FunctionMetaData.newBuilder().setFunctionDetails( | ||
Function.FunctionDetails.newBuilder().setName("func-1").setParallelism(2)).setVersion(version).build(); | ||
Function.FunctionMetaData newMetaData = FunctionMetaDataUtils.changeFunctionInstanceStatus(metaData, 0, false); | ||
Assert.assertTrue(newMetaData.getInstanceStatesMap() != null); | ||
Assert.assertEquals(newMetaData.getInstanceStatesMap().size(), 2); | ||
Assert.assertEquals(newMetaData.getInstanceStatesMap().get(0), Function.FunctionState.STOPPED); | ||
Assert.assertEquals(newMetaData.getInstanceStatesMap().get(1), Function.FunctionState.RUNNING); | ||
Assert.assertEquals(newMetaData.getVersion(), version + 1); | ||
|
||
// Nothing should happen | ||
newMetaData = FunctionMetaDataUtils.changeFunctionInstanceStatus(newMetaData, 3, false); | ||
Assert.assertTrue(newMetaData.getInstanceStatesMap() != null); | ||
Assert.assertEquals(newMetaData.getInstanceStatesMap().size(), 2); | ||
Assert.assertEquals(newMetaData.getInstanceStatesMap().get(0), Function.FunctionState.STOPPED); | ||
Assert.assertEquals(newMetaData.getInstanceStatesMap().get(1), Function.FunctionState.RUNNING); | ||
Assert.assertEquals(newMetaData.getVersion(), version + 2); | ||
|
||
// Change one more | ||
newMetaData = FunctionMetaDataUtils.changeFunctionInstanceStatus(newMetaData, 1, false); | ||
Assert.assertTrue(newMetaData.getInstanceStatesMap() != null); | ||
Assert.assertEquals(newMetaData.getInstanceStatesMap().size(), 2); | ||
Assert.assertEquals(newMetaData.getInstanceStatesMap().get(0), Function.FunctionState.STOPPED); | ||
Assert.assertEquals(newMetaData.getInstanceStatesMap().get(1), Function.FunctionState.STOPPED); | ||
Assert.assertEquals(newMetaData.getVersion(), version + 3); | ||
|
||
// Change all more | ||
newMetaData = FunctionMetaDataUtils.changeFunctionInstanceStatus(newMetaData, -1, true); | ||
Assert.assertTrue(newMetaData.getInstanceStatesMap() != null); | ||
Assert.assertEquals(newMetaData.getInstanceStatesMap().size(), 2); | ||
Assert.assertEquals(newMetaData.getInstanceStatesMap().get(0), Function.FunctionState.RUNNING); | ||
Assert.assertEquals(newMetaData.getInstanceStatesMap().get(1), Function.FunctionState.RUNNING); | ||
Assert.assertEquals(newMetaData.getVersion(), version + 4); | ||
} | ||
|
||
@Test | ||
public void testUpdate() { | ||
long version = 5; | ||
Function.FunctionMetaData existingMetaData = Function.FunctionMetaData.newBuilder().setFunctionDetails( | ||
Function.FunctionDetails.newBuilder().setName("func-1").setParallelism(2)).setVersion(version).build(); | ||
Function.FunctionMetaData updatedMetaData = Function.FunctionMetaData.newBuilder().setFunctionDetails( | ||
Function.FunctionDetails.newBuilder().setName("func-1").setParallelism(3)).setVersion(version).build(); | ||
Function.FunctionMetaData newMetaData = FunctionMetaDataUtils.generateUpdatedMetadata(existingMetaData, updatedMetaData); | ||
Assert.assertEquals(newMetaData.getVersion(), version + 1); | ||
Assert.assertEquals(newMetaData.getFunctionDetails().getParallelism(), 3); | ||
|
||
newMetaData = FunctionMetaDataUtils.generateUpdatedMetadata(null, newMetaData); | ||
Assert.assertEquals(newMetaData.getVersion(), 0); | ||
} | ||
} |
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