You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2022/10/18 01:42:50 UTC

[GitHub] [pulsar] tisonkun commented on a diff in pull request #18079: [improve][load-balance][pip-192] Added ServiceUnitStateChannel interface

tisonkun commented on code in PR #18079:
URL: https://github.com/apache/pulsar/pull/18079#discussion_r997639325


##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensible/channel/ServiceUnitState.java:
##########
@@ -0,0 +1,99 @@
+/**
+ * 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.broker.loadbalance.extensible.channel;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Defines the possible states for service units.
+ *
+ * The following diagram defines the valid state changes
+ *
+ *                  ┌───────────┐
+ *       ┌──────────┤ released  │◄────────┐
+ *       │own       └───────────┘         │release
+ *       │                                │
+ *       │                                │
+ *       ▼                                │
+ *    ┌────────┐    assign          ┌─────┴────┐
+ *    │        ├───────────────────►│          │
+ *    │ owned  │                    │ assigned │
+ *    │        │◄───────────────────┤          │
+ *    └──┬─────┤      own           └──────────┘
+ *       │  ▲  │                         ▲
+ *       │  │  │                         │
+ *       │  │  └──────────────┐          │
+ *       │  │                 │          │
+ *       │  │        unload   │          │ assign
+ * split │  │                 │          │
+ *       │  │                 │          │
+ *       │  │ create(child)   │          │
+ *       │  │                 │          │
+ *       ▼  │                 │          │
+ *    ┌─────┴─────┐           └─────►┌───┴──────┐
+ *    │           │                  │          │
+ *    │ splitting ├────────────────► │not-owned │
+ *    │           │   discard(parent)│          │
+ *    └───────────┘                  └──────────┘
+ */
+public enum ServiceUnitState {
+
+    Owned, // the ownership assignment is complete (terminal state)
+
+    Assigned, // the ownership is assigned(but the assigned broker has not taken the ownership yet)
+
+    Released, // the ownership is released (e.g. the topic connections are closed)
+
+    Splitting; // the service unit(e.g. bundle) is in the process of splitting.
+    private static Map<ServiceUnitState, Set<ServiceUnitState>> validTransitions = new HashMap<>() {{
+        put(null, new HashSet<>() {{

Review Comment:
   Shall we define a state `Free`/`NotOwned` over `null`? Then we can avoid handling business `null`s.



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensible/channel/ServiceUnitStateData.java:
##########
@@ -0,0 +1,50 @@
+/**
+ * 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.broker.loadbalance.extensible.channel;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import lombok.ToString;
+
+/**
+ * Defines data for the service unit state changes.
+ * This data will be broadcast in ServiceUnitStateChannel.
+ */
+@Data
+@ToString
+@NoArgsConstructor
+@AllArgsConstructor
+public class ServiceUnitStateData {
+    private ServiceUnitState state;
+    private String broker;
+    private String sourceBroker;
+    private long timestamp;

Review Comment:
   1. `@Data` already infers `@ToString`.
   2. Since the broker JDK version is upgraded to 17, do you think of using [record class](https://docs.oracle.com/en/java/javase/17/language/records.html) to define data models?
   3. Are these field intended to be mutable?



##########
pulsar-broker/src/test/java/org/apache/pulsar/broker/loadbalance/extensible/channel/ServiceUnitStateDataTest.java:
##########
@@ -0,0 +1,46 @@
+/**
+ * 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.broker.loadbalance.extensible.channel;
+
+import static org.apache.pulsar.broker.loadbalance.extensible.channel.ServiceUnitState.Assigned;
+import static org.apache.pulsar.broker.loadbalance.extensible.channel.ServiceUnitState.Owned;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNull;
+import static org.testng.Assert.assertTrue;
+import org.testng.annotations.Test;
+
+@Test(groups = "broker")
+public class ServiceUnitStateDataTest {
+
+    @Test
+    public void testConstructors() {
+        ServiceUnitStateData data1 = new ServiceUnitStateData(Owned, "A");
+        assertEquals(data1.getState(), Owned);
+        assertEquals(data1.getBroker(), "A");
+        assertNull(data1.getSourceBroker());
+        assertTrue(data1.getTimestamp() > 0);

Review Comment:
   ditto other assertions.



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensible/channel/ServiceUnitState.java:
##########
@@ -0,0 +1,99 @@
+/**
+ * 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.broker.loadbalance.extensible.channel;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Defines the possible states for service units.
+ *
+ * The following diagram defines the valid state changes
+ *
+ *                  ┌───────────┐
+ *       ┌──────────┤ released  │◄────────┐
+ *       │own       └───────────┘         │release
+ *       │                                │
+ *       │                                │
+ *       ▼                                │
+ *    ┌────────┐    assign          ┌─────┴────┐
+ *    │        ├───────────────────►│          │
+ *    │ owned  │                    │ assigned │
+ *    │        │◄───────────────────┤          │
+ *    └──┬─────┤      own           └──────────┘
+ *       │  ▲  │                         ▲
+ *       │  │  │                         │
+ *       │  │  └──────────────┐          │
+ *       │  │                 │          │
+ *       │  │        unload   │          │ assign
+ * split │  │                 │          │
+ *       │  │                 │          │
+ *       │  │ create(child)   │          │
+ *       │  │                 │          │
+ *       ▼  │                 │          │
+ *    ┌─────┴─────┐           └─────►┌───┴──────┐
+ *    │           │                  │          │
+ *    │ splitting ├────────────────► │not-owned │
+ *    │           │   discard(parent)│          │
+ *    └───────────┘                  └──────────┘
+ */
+public enum ServiceUnitState {
+
+    Owned, // the ownership assignment is complete (terminal state)
+
+    Assigned, // the ownership is assigned(but the assigned broker has not taken the ownership yet)
+
+    Released, // the ownership is released (e.g. the topic connections are closed)
+
+    Splitting; // the service unit(e.g. bundle) is in the process of splitting.
+    private static Map<ServiceUnitState, Set<ServiceUnitState>> validTransitions = new HashMap<>() {{
+        put(null, new HashSet<>() {{

Review Comment:
   I'd prefer immutable sets over `HashSet` here. For example, `Set.of(...)`.



##########
pulsar-broker/src/test/java/org/apache/pulsar/broker/loadbalance/extensible/channel/ServiceUnitStateDataTest.java:
##########
@@ -0,0 +1,46 @@
+/**
+ * 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.broker.loadbalance.extensible.channel;
+
+import static org.apache.pulsar.broker.loadbalance.extensible.channel.ServiceUnitState.Assigned;
+import static org.apache.pulsar.broker.loadbalance.extensible.channel.ServiceUnitState.Owned;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNull;
+import static org.testng.Assert.assertTrue;
+import org.testng.annotations.Test;
+
+@Test(groups = "broker")
+public class ServiceUnitStateDataTest {
+
+    @Test
+    public void testConstructors() {
+        ServiceUnitStateData data1 = new ServiceUnitStateData(Owned, "A");
+        assertEquals(data1.getState(), Owned);
+        assertEquals(data1.getBroker(), "A");
+        assertNull(data1.getSourceBroker());
+        assertTrue(data1.getTimestamp() > 0);

Review Comment:
   You can use `assertj`'s assertion for better error message:
   
   ```java
   import static org.assertj.core.api.Assertions.assertThat;
   
   assertThat(data1.getTimestamp()).isGreaterThan(0);
   ```



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org