forked from IBM/sarama
-
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.
refactor ConsumerMetadataRequest/Response to FindCoordinatorRequest/R…
…esponse
- Loading branch information
Robin
committed
Dec 20, 2017
1 parent
d5db635
commit a5a9b83
Showing
10 changed files
with
317 additions
and
60 deletions.
There are no files selected for viewing
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
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,61 @@ | ||
package sarama | ||
|
||
type CoordinatorType int8 | ||
|
||
const ( | ||
CoordinatorGroup CoordinatorType = 0 | ||
CoordinatorTransaction CoordinatorType = 1 | ||
) | ||
|
||
type FindCoordinatorRequest struct { | ||
Version int16 | ||
CoordinatorKey string | ||
CoordinatorType CoordinatorType | ||
} | ||
|
||
func (f *FindCoordinatorRequest) encode(pe packetEncoder) error { | ||
if err := pe.putString(f.CoordinatorKey); err != nil { | ||
return err | ||
} | ||
|
||
if f.Version >= 1 { | ||
pe.putInt8(int8(f.CoordinatorType)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (f *FindCoordinatorRequest) decode(pd packetDecoder, version int16) (err error) { | ||
if f.CoordinatorKey, err = pd.getString(); err != nil { | ||
return err | ||
} | ||
|
||
if version >= 1 { | ||
f.Version = version | ||
coordinatorType, err := pd.getInt8() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
f.CoordinatorType = CoordinatorType(coordinatorType) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (f *FindCoordinatorRequest) key() int16 { | ||
return 10 | ||
} | ||
|
||
func (f *FindCoordinatorRequest) version() int16 { | ||
return f.Version | ||
} | ||
|
||
func (f *FindCoordinatorRequest) requiredVersion() KafkaVersion { | ||
switch f.Version { | ||
case 1: | ||
return V0_11_0_0 | ||
default: | ||
return V0_8_2_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package sarama | ||
|
||
import "testing" | ||
|
||
var ( | ||
findCoordinatorRequestConsumerGroup = []byte{ | ||
0, 5, 'g', 'r', 'o', 'u', 'p', | ||
0, | ||
} | ||
|
||
findCoordinatorRequestTransaction = []byte{ | ||
0, 13, 't', 'r', 'a', 'n', 's', 'a', 'c', 't', 'i', 'o', 'n', 'i', 'd', | ||
1, | ||
} | ||
) | ||
|
||
func TestFindCoordinatorRequest(t *testing.T) { | ||
req := &FindCoordinatorRequest{ | ||
Version: 1, | ||
CoordinatorKey: "group", | ||
CoordinatorType: CoordinatorGroup, | ||
} | ||
|
||
testRequest(t, "version 1 - group", req, findCoordinatorRequestConsumerGroup) | ||
|
||
req = &FindCoordinatorRequest{ | ||
Version: 1, | ||
CoordinatorKey: "transactionid", | ||
CoordinatorType: CoordinatorTransaction, | ||
} | ||
|
||
testRequest(t, "version 1 - transaction", req, findCoordinatorRequestTransaction) | ||
} |
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,121 @@ | ||
package sarama | ||
|
||
import ( | ||
"net" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
type FindCoordinatorResponse struct { | ||
Version int16 | ||
ThrottleTime time.Duration | ||
Err KError | ||
ErrMsg *string | ||
Coordinator *Broker | ||
CoordinatorID int32 // deprecated: use Coordinator.ID() | ||
CoordinatorHost string // deprecated: use Coordinator.Addr() | ||
CoordinatorPort int32 // deprecated: use Coordinator.Addr() | ||
} | ||
|
||
func (f *FindCoordinatorResponse) decode(pd packetDecoder, version int16) (err error) { | ||
if version >= 1 { | ||
f.Version = version | ||
|
||
throttleTime, err := pd.getInt32() | ||
if err != nil { | ||
return err | ||
} | ||
f.ThrottleTime = time.Duration(throttleTime) * time.Millisecond | ||
} | ||
|
||
tmp, err := pd.getInt16() | ||
if err != nil { | ||
return err | ||
} | ||
f.Err = KError(tmp) | ||
|
||
if version >= 1 { | ||
if f.ErrMsg, err = pd.getNullableString(); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
coordinator := new(Broker) | ||
if err := coordinator.decode(pd); err != nil { | ||
return err | ||
} | ||
if coordinator.addr == ":0" { | ||
return nil | ||
} | ||
f.Coordinator = coordinator | ||
|
||
// this can all go away in 2.0, but we have to fill in deprecated fields to maintain | ||
// backwards compatibility | ||
host, portstr, err := net.SplitHostPort(f.Coordinator.Addr()) | ||
if err != nil { | ||
return err | ||
} | ||
port, err := strconv.ParseInt(portstr, 10, 32) | ||
if err != nil { | ||
return err | ||
} | ||
f.CoordinatorID = f.Coordinator.ID() | ||
f.CoordinatorHost = host | ||
f.CoordinatorPort = int32(port) | ||
|
||
return nil | ||
} | ||
|
||
func (f *FindCoordinatorResponse) encode(pe packetEncoder) error { | ||
if f.Version >= 1 { | ||
pe.putInt32(int32(f.ThrottleTime / time.Millisecond)) | ||
} | ||
|
||
pe.putInt16(int16(f.Err)) | ||
|
||
if f.Version >= 1 { | ||
if err := pe.putNullableString(f.ErrMsg); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if f.Coordinator != nil { | ||
host, portstr, err := net.SplitHostPort(f.Coordinator.Addr()) | ||
if err != nil { | ||
return err | ||
} | ||
port, err := strconv.ParseInt(portstr, 10, 32) | ||
if err != nil { | ||
return err | ||
} | ||
pe.putInt32(f.Coordinator.ID()) | ||
if err := pe.putString(host); err != nil { | ||
return err | ||
} | ||
pe.putInt32(int32(port)) | ||
return nil | ||
} | ||
pe.putInt32(f.CoordinatorID) | ||
if err := pe.putString(f.CoordinatorHost); err != nil { | ||
return err | ||
} | ||
pe.putInt32(f.CoordinatorPort) | ||
return nil | ||
} | ||
|
||
func (f *FindCoordinatorResponse) key() int16 { | ||
return 10 | ||
} | ||
|
||
func (f *FindCoordinatorResponse) version() int16 { | ||
return f.Version | ||
} | ||
|
||
func (f *FindCoordinatorResponse) requiredVersion() KafkaVersion { | ||
switch f.Version { | ||
case 1: | ||
return V0_11_0_0 | ||
default: | ||
return V0_8_2_0 | ||
} | ||
} |
Oops, something went wrong.