Skip to content

Commit b9db02d

Browse files
committed
[FAB-1360] Introduce ChainPartition for Kafka
https://jira.hyperledger.org/browse/FAB-1360 This changeset introduces ChainPartition, a construct that will be used to identify the Kafka topic/partition that the ordering shims should interact with when dealing with a particular chain. Note that it encodes the given chain ID in base-16 as a means of escaping characters that are not allowed in Kafka topic names. All the restrictions for topic names are linked to in line 38. Filtering for complying chain IDs should probably happen during transaction filtering at ingress. This is marked as a TODO, and a JIRA issue has been created for it. This changeset merely introduces the ChainPartition construct and does not integrate it with the rest of the code yet. This will happen in a follow-up changeset. Change-Id: I33d4e80758d7dfd7e7d827caa385d0507a39a40a Signed-off-by: Kostas Christidis <[email protected]>
1 parent 95094cd commit b9db02d

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

orderer/kafka/chain_partition.go

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Copyright IBM Corp. 2016 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package kafka
18+
19+
import "fmt"
20+
21+
const rawPartition = 0
22+
23+
// ChainPartition identifies the Kafka partition the orderer interacts with.
24+
type ChainPartition interface {
25+
Topic() string
26+
Partition() int32
27+
fmt.Stringer
28+
}
29+
30+
type chainPartitionImpl struct {
31+
tpc string
32+
prt int32
33+
}
34+
35+
// Returns a new chain partition for a given chain ID and partition.
36+
func newChainPartition(chainID string, partition int32) ChainPartition {
37+
return &chainPartitionImpl{
38+
// TODO https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/common/Topic.scala#L29
39+
tpc: fmt.Sprintf("%x", chainID),
40+
prt: partition,
41+
}
42+
}
43+
44+
// Topic returns the Kafka topic of this chain partition.
45+
func (cp *chainPartitionImpl) Topic() string {
46+
return cp.tpc
47+
}
48+
49+
// Partition returns the Kafka partition of this chain partition.
50+
func (cp *chainPartitionImpl) Partition() int32 {
51+
return cp.prt
52+
}
53+
54+
// String returns a string identifying the chain partition.
55+
func (cp *chainPartitionImpl) String() string {
56+
return fmt.Sprintf("%s/%d", cp.tpc, cp.prt)
57+
}

orderer/kafka/chain_partition_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Copyright IBM Corp. 2016 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package kafka
18+
19+
import (
20+
"fmt"
21+
"strings"
22+
"testing"
23+
24+
"github.com/hyperledger/fabric/orderer/common/bootstrap/static"
25+
)
26+
27+
func TestChainPartition(t *testing.T) {
28+
cp := newChainPartition(static.TestChainID, rawPartition)
29+
30+
expectedTopic := fmt.Sprintf("%x", static.TestChainID)
31+
actualTopic := cp.Topic()
32+
if strings.Compare(expectedTopic, actualTopic) != 0 {
33+
t.Fatalf("Got the wrong topic, expected %s, got %s instead", expectedTopic, actualTopic)
34+
}
35+
36+
expectedPartition := int32(rawPartition)
37+
actualPartition := cp.Partition()
38+
if actualPartition != expectedPartition {
39+
t.Fatalf("Got the wrong partition, expected %d, got %d instead", expectedPartition, actualPartition)
40+
}
41+
}

0 commit comments

Comments
 (0)