Commit 346454a3 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 261f9303
...@@ -81,6 +81,18 @@ type UUID int32 ...@@ -81,6 +81,18 @@ type UUID int32
// TODO UUID_NAMESPACES // TODO UUID_NAMESPACES
// NEOEncoder is interface for marshaling objects to wire format in NEO
type NEOEncoder interface {
// NEOEncodedLen() int ?
// XXX len(buf) must be >= NEOEncodedLen()
NEOEncode(buf []byte) (nwrote int)
}
// NEODecoder is interface for unmarshalling objects from wire format in NEO
type NEODecoder interface {
NEODecode(data []byte) (nread int)
}
type Address struct { type Address struct {
Host string Host string
Port uint16 Port uint16
...@@ -88,7 +100,7 @@ type Address struct { ...@@ -88,7 +100,7 @@ type Address struct {
// NOTE if Host == "" -> Port not added to wire (see py.PAddress): // NOTE if Host == "" -> Port not added to wire (see py.PAddress):
func (a *Address) NEOEncode(b []byte) int { func (a *Address) NEOEncode(b []byte) int {
n := a.Host.NEOEncode(b[0:]) n := string_NEOEncode(a.Host, b[0:])
if a.Host != "" { if a.Host != "" {
BigEndian.PutUint16(b[n:], a.Port) BigEndian.PutUint16(b[n:], a.Port)
n += 2 n += 2
...@@ -97,9 +109,10 @@ func (a *Address) NEOEncode(b []byte) int { ...@@ -97,9 +109,10 @@ func (a *Address) NEOEncode(b []byte) int {
} }
func (a *Address) NEODecode(b []byte) int { func (a *Address) NEODecode(b []byte) int {
n := a.Host.NEODecode(b) n := string_NEODecode(&a.Host, b)
if a.Host != "" { if a.Host != "" {
n += a.Port.NEODecode(b[n:]) a.Port = BigEndian.Uint16(b[n:])
n += 2
} else { } else {
a.Port = 0 a.Port = 0
} }
...@@ -115,7 +128,7 @@ type PTid uint64 // XXX move to common place ? ...@@ -115,7 +128,7 @@ type PTid uint64 // XXX move to common place ?
type Float64 float64 type Float64 float64
// NOTE py.None encodes as '\xff' * 8 (-> use NaN for None) // NOTE py.None encodes as '\xff' * 8 (-> we use NaN for None)
// NOTE '\xff' * 8 represents FP NaN but many other NaN bits representation exist // NOTE '\xff' * 8 represents FP NaN but many other NaN bits representation exist
func (f Float64) NEOEncode(b []byte) int { func (f Float64) NEOEncode(b []byte) int {
var fu uint64 var fu uint64
...@@ -171,7 +184,6 @@ type PktHead struct { ...@@ -171,7 +184,6 @@ type PktHead struct {
// General purpose notification (remote logging) // General purpose notification (remote logging)
type Notify struct { type Notify struct {
PktHead
Message string Message string
} }
...@@ -179,26 +191,22 @@ type Notify struct { ...@@ -179,26 +191,22 @@ type Notify struct {
// any other message, even if such a message does not expect a reply // any other message, even if such a message does not expect a reply
// usually. Any -> Any. // usually. Any -> Any.
type Error struct { type Error struct {
PktHead
Code uint32 // PNumber Code uint32 // PNumber
Message string Message string
} }
// Check if a peer is still alive. Any -> Any. // Check if a peer is still alive. Any -> Any.
type Ping struct { type Ping struct {
PktHead
// TODO _answer = PFEmpty // TODO _answer = PFEmpty
} }
// Tell peer it can close the connection if it has finished with us. Any -> Any // Tell peer it can close the connection if it has finished with us. Any -> Any
type CloseClient struct { type CloseClient struct {
PktHead
} }
// Request a node identification. This must be the first packet for any // Request a node identification. This must be the first packet for any
// connection. Any -> Any. // connection. Any -> Any.
type RequestIdentification struct { type RequestIdentification struct {
PktHead
ProtocolVersion uint32 // TODO py.PProtocol upon decoding checks for != PROTOCOL_VERSION ProtocolVersion uint32 // TODO py.PProtocol upon decoding checks for != PROTOCOL_VERSION
NodeType NodeType // XXX name NodeType NodeType // XXX name
UUID UUID UUID UUID
...@@ -209,7 +217,6 @@ type RequestIdentification struct { ...@@ -209,7 +217,6 @@ type RequestIdentification struct {
// XXX -> ReplyIdentification? RequestIdentification.Answer somehow ? // XXX -> ReplyIdentification? RequestIdentification.Answer somehow ?
type AcceptIdentification struct { type AcceptIdentification struct {
PktHead
NodeType NodeType // XXX name NodeType NodeType // XXX name
MyUUID UUID MyUUID UUID
NumPartitions uint32 // PNumber NumPartitions uint32 // PNumber
...@@ -224,31 +231,25 @@ type AcceptIdentification struct { ...@@ -224,31 +231,25 @@ type AcceptIdentification struct {
// Ask current primary master's uuid. CTL -> A. // Ask current primary master's uuid. CTL -> A.
type PrimaryMaster struct { type PrimaryMaster struct {
PktHead
} }
type AnswerPrimary struct { type AnswerPrimary struct {
PktHead
PrimaryUUID UUID PrimaryUUID UUID
} }
// Announce a primary master node election. PM -> SM. // Announce a primary master node election. PM -> SM.
type AnnouncePrimary struct { type AnnouncePrimary struct {
PktHead
} }
// Force a re-election of a primary master node. M -> M. // Force a re-election of a primary master node. M -> M.
type ReelectPrimary struct { type ReelectPrimary struct {
PktHead
} }
// Ask all data needed by master to recover. PM -> S, S -> PM. // Ask all data needed by master to recover. PM -> S, S -> PM.
type Recovery struct { type Recovery struct {
PktHead
} }
type AnswerRecovery struct { type AnswerRecovery struct {
PktHead
PTid PTid
BackupTID Tid BackupTID Tid
TruncateTID Tid TruncateTID Tid
...@@ -257,11 +258,9 @@ type AnswerRecovery struct { ...@@ -257,11 +258,9 @@ type AnswerRecovery struct {
// Ask the last OID/TID so that a master can initialize its TransactionManager. // Ask the last OID/TID so that a master can initialize its TransactionManager.
// PM -> S, S -> PM. // PM -> S, S -> PM.
type LastIDs struct { type LastIDs struct {
PktHead
} }
type AnswerLastIDs struct { type AnswerLastIDs struct {
PktHead
LastOID Oid LastOID Oid
LastTID Tid LastTID Tid
} }
...@@ -269,11 +268,9 @@ type AnswerLastIDs struct { ...@@ -269,11 +268,9 @@ type AnswerLastIDs struct {
// Ask the full partition table. PM -> S. // Ask the full partition table. PM -> S.
// Answer rows in a partition table. S -> PM. // Answer rows in a partition table. S -> PM.
type PartitionTable struct { type PartitionTable struct {
PktHead
} }
type AnswerPartitionTable struct { type AnswerPartitionTable struct {
PktHead
PTid PTid
RowList []RowInfo RowList []RowInfo
} }
...@@ -281,7 +278,6 @@ type AnswerPartitionTable struct { ...@@ -281,7 +278,6 @@ type AnswerPartitionTable struct {
// Send rows in a partition table to update other nodes. PM -> S, C. // Send rows in a partition table to update other nodes. PM -> S, C.
type NotifyPartitionTable struct { type NotifyPartitionTable struct {
PktHead
PTid PTid
RowList []RowInfo RowList []RowInfo
} }
...@@ -289,8 +285,6 @@ type NotifyPartitionTable struct { ...@@ -289,8 +285,6 @@ type NotifyPartitionTable struct {
// Notify a subset of a partition table. This is used to notify changes. // Notify a subset of a partition table. This is used to notify changes.
// PM -> S, C. // PM -> S, C.
type PartitionChanges struct { type PartitionChanges struct {
PktHead
PTid PTid
CellList []struct { CellList []struct {
// XXX does below correlate with Cell inside top-level CellList ? // XXX does below correlate with Cell inside top-level CellList ?
...@@ -303,7 +297,6 @@ type PartitionChanges struct { ...@@ -303,7 +297,6 @@ type PartitionChanges struct {
// Tell a storage nodes to start an operation. Until a storage node receives // Tell a storage nodes to start an operation. Until a storage node receives
// this message, it must not serve client nodes. PM -> S. // this message, it must not serve client nodes. PM -> S.
type StartOperation struct { type StartOperation struct {
PktHead
// XXX: Is this boolean needed ? Maybe this // XXX: Is this boolean needed ? Maybe this
// can be deduced from cluster state. // can be deduced from cluster state.
Backup bool Backup bool
...@@ -312,17 +305,14 @@ type StartOperation struct { ...@@ -312,17 +305,14 @@ type StartOperation struct {
// Tell a storage node to stop an operation. Once a storage node receives // Tell a storage node to stop an operation. Once a storage node receives
// this message, it must not serve client nodes. PM -> S. // this message, it must not serve client nodes. PM -> S.
type StopOperation struct { type StopOperation struct {
PktHead
} }
// Ask unfinished transactions S -> PM. // Ask unfinished transactions S -> PM.
// Answer unfinished transactions PM -> S. // Answer unfinished transactions PM -> S.
type UnfinishedTransactions struct { type UnfinishedTransactions struct {
PktHead
} }
type AnswerUnfinishedTransactions struct { type AnswerUnfinishedTransactions struct {
PktHead
MaxTID Tid MaxTID Tid
TidList []struct{ TidList []struct{
UnfinishedTID Tid UnfinishedTID Tid
...@@ -332,28 +322,23 @@ type AnswerUnfinishedTransactions struct { ...@@ -332,28 +322,23 @@ type AnswerUnfinishedTransactions struct {
// Ask locked transactions PM -> S. // Ask locked transactions PM -> S.
// Answer locked transactions S -> PM. // Answer locked transactions S -> PM.
type LockedTransactions struct { type LockedTransactions struct {
PktHead
} }
type AnswerLockedTransactions struct { type AnswerLockedTransactions struct {
PktHead
TidDict map[Tid]Tid // ttid -> tid TidDict map[Tid]Tid // ttid -> tid
} }
// Return final tid if ttid has been committed. * -> S. C -> PM. // Return final tid if ttid has been committed. * -> S. C -> PM.
type FinalTID struct { type FinalTID struct {
PktHead
TTID Tid TTID Tid
} }
type AnswerFinalTID struct { type AnswerFinalTID struct {
PktHead
Tid Tid Tid Tid
} }
// Commit a transaction. PM -> S. // Commit a transaction. PM -> S.
type ValidateTransaction struct { type ValidateTransaction struct {
PktHead
TTID Tid TTID Tid
Tid Tid Tid Tid
} }
...@@ -362,26 +347,22 @@ type ValidateTransaction struct { ...@@ -362,26 +347,22 @@ type ValidateTransaction struct {
// Ask to begin a new transaction. C -> PM. // Ask to begin a new transaction. C -> PM.
// Answer when a transaction begin, give a TID if necessary. PM -> C. // Answer when a transaction begin, give a TID if necessary. PM -> C.
type BeginTransaction struct { type BeginTransaction struct {
PktHead
Tid Tid Tid Tid
} }
type AnswerBeginTransaction struct { type AnswerBeginTransaction struct {
PktHead
Tid Tid Tid Tid
} }
// Finish a transaction. C -> PM. // Finish a transaction. C -> PM.
// Answer when a transaction is finished. PM -> C. // Answer when a transaction is finished. PM -> C.
type FinishTransaction struct { type FinishTransaction struct {
PktHead
Tid Tid Tid Tid
OIDList []Oid OIDList []Oid
CheckedList []Oid CheckedList []Oid
} }
type AnswerFinishTransaction struct { type AnswerFinishTransaction struct {
PktHead
TTID Tid TTID Tid
Tid Tid Tid Tid
} }
...@@ -389,7 +370,6 @@ type AnswerFinishTransaction struct { ...@@ -389,7 +370,6 @@ type AnswerFinishTransaction struct {
// Notify that a transaction blocking a replication is now finished // Notify that a transaction blocking a replication is now finished
// M -> S // M -> S
type NotifyTransactionFinished struct { type NotifyTransactionFinished struct {
PktHead
TTID Tid TTID Tid
MaxTID Tid MaxTID Tid
} }
...@@ -397,7 +377,6 @@ type NotifyTransactionFinished struct { ...@@ -397,7 +377,6 @@ type NotifyTransactionFinished struct {
// Lock information on a transaction. PM -> S. // Lock information on a transaction. PM -> S.
// Notify information on a transaction locked. S -> PM. // Notify information on a transaction locked. S -> PM.
type LockInformation struct { type LockInformation struct {
PktHead
Ttid Tid Ttid Tid
Tid Tid Tid Tid
} }
...@@ -410,27 +389,23 @@ type AnswerLockInformation struct { ...@@ -410,27 +389,23 @@ type AnswerLockInformation struct {
// Invalidate objects. PM -> C. // Invalidate objects. PM -> C.
// XXX ask_finish_transaction ? // XXX ask_finish_transaction ?
type InvalidateObjects struct { type InvalidateObjects struct {
PktHead
Tid Tid Tid Tid
OidList []Oid OidList []Oid
} }
// Unlock information on a transaction. PM -> S. // Unlock information on a transaction. PM -> S.
type UnlockInformation struct { type UnlockInformation struct {
PktHead
TTID Tid TTID Tid
} }
// Ask new object IDs. C -> PM. // Ask new object IDs. C -> PM.
// Answer new object IDs. PM -> C. // Answer new object IDs. PM -> C.
type GenerateOIDs struct { type GenerateOIDs struct {
PktHead
NumOIDs uint32 // PNumber NumOIDs uint32 // PNumber
} }
// XXX answer_new_oids ? // XXX answer_new_oids ?
type AnswerGenerateOIDs struct { type AnswerGenerateOIDs struct {
PktHead
OidList []Oid OidList []Oid
} }
...@@ -442,7 +417,6 @@ type AnswerGenerateOIDs struct { ...@@ -442,7 +417,6 @@ type AnswerGenerateOIDs struct {
// if this serial is newer than the current transaction ID, a client // if this serial is newer than the current transaction ID, a client
// node must not try to resolve the conflict. S -> C. // node must not try to resolve the conflict. S -> C.
type StoreObject struct { type StoreObject struct {
PktHead
Oid Oid Oid Oid
Serial Tid Serial Tid
Compression bool Compression bool
...@@ -454,7 +428,6 @@ type StoreObject struct { ...@@ -454,7 +428,6 @@ type StoreObject struct {
} }
type AnswerStoreObject struct { type AnswerStoreObject struct {
PktHead
Conflicting bool Conflicting bool
Oid Oid Oid Oid
Serial Tid Serial Tid
...@@ -462,14 +435,12 @@ type AnswerStoreObject struct { ...@@ -462,14 +435,12 @@ type AnswerStoreObject struct {
// Abort a transaction. C -> S, PM. // Abort a transaction. C -> S, PM.
type AbortTransaction struct { type AbortTransaction struct {
PktHead
Tid Tid Tid Tid
} }
// Ask to store a transaction. C -> S. // Ask to store a transaction. C -> S.
// Answer if transaction has been stored. S -> C. // Answer if transaction has been stored. S -> C.
type StoreTransaction struct { type StoreTransaction struct {
PktHead
Tid Tid Tid Tid
User string User string
Description string Description string
...@@ -481,7 +452,6 @@ type StoreTransaction struct { ...@@ -481,7 +452,6 @@ type StoreTransaction struct {
// Ask to store a transaction. C -> S. // Ask to store a transaction. C -> S.
// Answer if transaction has been stored. S -> C. // Answer if transaction has been stored. S -> C.
type VoteTransaction struct { type VoteTransaction struct {
PktHead
Tid Tid Tid Tid
// TODO _answer = PFEmpty // TODO _answer = PFEmpty
} }
...@@ -491,7 +461,6 @@ type VoteTransaction struct { ...@@ -491,7 +461,6 @@ type VoteTransaction struct {
// a TID is specified, an object right before the TID will be returned. C -> S. // a TID is specified, an object right before the TID will be returned. C -> S.
// Answer the requested object. S -> C. // Answer the requested object. S -> C.
type GetObject struct { type GetObject struct {
PktHead
Oid Oid Oid Oid
Serial Tid Serial Tid
Tid Tid Tid Tid
...@@ -499,7 +468,6 @@ type GetObject struct { ...@@ -499,7 +468,6 @@ type GetObject struct {
// XXX answer_object ? // XXX answer_object ?
type AnswerGetObject struct { type AnswerGetObject struct {
PktHead
Oid Oid Oid Oid
SerialStart Tid SerialStart Tid
SerialEnd Tid SerialEnd Tid
...@@ -513,7 +481,6 @@ type AnswerGetObject struct { ...@@ -513,7 +481,6 @@ type AnswerGetObject struct {
// and the range is [first, last). C -> S. // and the range is [first, last). C -> S.
// Answer the requested TIDs. S -> C. // Answer the requested TIDs. S -> C.
type TIDList struct { type TIDList struct {
PktHead
First uint64 // PIndex XXX this is TID actually ? -> no it is offset in list First uint64 // PIndex XXX this is TID actually ? -> no it is offset in list
Last uint64 // PIndex ----//---- Last uint64 // PIndex ----//----
Partition uint32 // PNumber Partition uint32 // PNumber
...@@ -521,7 +488,6 @@ type TIDList struct { ...@@ -521,7 +488,6 @@ type TIDList struct {
// XXX answer_tids ? // XXX answer_tids ?
type AnswerTIDList struct { type AnswerTIDList struct {
PktHead
TIDList []Tid TIDList []Tid
} }
...@@ -529,7 +495,6 @@ type AnswerTIDList struct { ...@@ -529,7 +495,6 @@ type AnswerTIDList struct {
// C -> S. // C -> S.
// Answer the requested TIDs. S -> C // Answer the requested TIDs. S -> C
type TIDListFrom struct { type TIDListFrom struct {
PktHead
MinTID Tid MinTID Tid
MaxTID Tid MaxTID Tid
Length uint32 // PNumber Length uint32 // PNumber
...@@ -538,19 +503,16 @@ type TIDListFrom struct { ...@@ -538,19 +503,16 @@ type TIDListFrom struct {
// XXX answer_tids ? // XXX answer_tids ?
type AnswerTIDListFrom struct { type AnswerTIDListFrom struct {
PktHead
TidList []Tid TidList []Tid
} }
// Ask information about a transaction. Any -> S. // Ask information about a transaction. Any -> S.
// Answer information (user, description) about a transaction. S -> Any. // Answer information (user, description) about a transaction. S -> Any.
type TransactionInformation struct { type TransactionInformation struct {
PktHead
Tid Tid Tid Tid
} }
type AnswerTransactionInformation struct { type AnswerTransactionInformation struct {
PktHead
Tid Tid Tid Tid
User string User string
Description string Description string
...@@ -563,14 +525,12 @@ type AnswerTransactionInformation struct { ...@@ -563,14 +525,12 @@ type AnswerTransactionInformation struct {
// descending, and the range is [first, last]. C -> S. // descending, and the range is [first, last]. C -> S.
// Answer history information (serial, size) for an object. S -> C. // Answer history information (serial, size) for an object. S -> C.
type ObjectHistory struct { type ObjectHistory struct {
PktHead
Oid Oid Oid Oid
First uint64 // PIndex XXX this is actually TID First uint64 // PIndex XXX this is actually TID
Last uint64 // PIndex ----//---- Last uint64 // PIndex ----//----
} }
type AnswerObjectHistory struct { type AnswerObjectHistory struct {
PktHead
Oid Oid Oid Oid
HistoryList []struct { HistoryList []struct {
Serial Tid Serial Tid
...@@ -582,14 +542,12 @@ type AnswerObjectHistory struct { ...@@ -582,14 +542,12 @@ type AnswerObjectHistory struct {
// Ask information about partition // Ask information about partition
// Answer information about partition // Answer information about partition
type PartitionList struct { type PartitionList struct {
PktHead
MinOffset uint32 // PNumber MinOffset uint32 // PNumber
MaxOffset uint32 // PNumber MaxOffset uint32 // PNumber
UUID UUID UUID UUID
} }
type AnswerPartitionList struct { type AnswerPartitionList struct {
PktHead
PTid PTid
RowList []RowInfo RowList []RowInfo
} }
...@@ -597,18 +555,15 @@ type AnswerPartitionList struct { ...@@ -597,18 +555,15 @@ type AnswerPartitionList struct {
// Ask information about nodes // Ask information about nodes
// Answer information about nodes // Answer information about nodes
type X_NodeList struct { type X_NodeList struct {
PktHead
NodeType NodeType
} }
type AnswerNodeList struct { type AnswerNodeList struct {
PktHead
NodeList []NodeInfo NodeList []NodeInfo
} }
// Set the node state // Set the node state
type SetNodeState struct { type SetNodeState struct {
PktHead
UUID UUID
NodeState NodeState
...@@ -617,7 +572,6 @@ type SetNodeState struct { ...@@ -617,7 +572,6 @@ type SetNodeState struct {
// Ask the primary to include some pending node in the partition table // Ask the primary to include some pending node in the partition table
type AddPendingNodes struct { type AddPendingNodes struct {
PktHead
UUIDList []UUID UUIDList []UUID
// XXX _answer = Error // XXX _answer = Error
...@@ -625,7 +579,6 @@ type AddPendingNodes struct { ...@@ -625,7 +579,6 @@ type AddPendingNodes struct {
// Ask the primary to optimize the partition table. A -> PM. // Ask the primary to optimize the partition table. A -> PM.
type TweakPartitionTable struct { type TweakPartitionTable struct {
PktHead
UUIDList []UUID UUIDList []UUID
// XXX _answer = Error // XXX _answer = Error
...@@ -633,19 +586,16 @@ type TweakPartitionTable struct { ...@@ -633,19 +586,16 @@ type TweakPartitionTable struct {
// Notify information about one or more nodes. PM -> Any. // Notify information about one or more nodes. PM -> Any.
type NotifyNodeInformation struct { type NotifyNodeInformation struct {
PktHead
NodeList []NodeInfo NodeList []NodeInfo
} }
// Ask node information // Ask node information
type NodeInformation struct { type NodeInformation struct {
PktHead
// XXX _answer = PFEmpty // XXX _answer = PFEmpty
} }
// Set the cluster state // Set the cluster state
type SetClusterState struct { type SetClusterState struct {
PktHead
State ClusterState State ClusterState
// XXX _answer = Error // XXX _answer = Error
...@@ -653,14 +603,12 @@ type SetClusterState struct { ...@@ -653,14 +603,12 @@ type SetClusterState struct {
// Notify information about the cluster // Notify information about the cluster
type ClusterInformation struct { type ClusterInformation struct {
PktHead
State ClusterState State ClusterState
} }
// Ask state of the cluster // Ask state of the cluster
// Answer state of the cluster // Answer state of the cluster
type X_ClusterState struct { // XXX conflicts with ClusterState enum type X_ClusterState struct { // XXX conflicts with ClusterState enum
PktHead
State ClusterState State ClusterState
} }
...@@ -680,7 +628,6 @@ type X_ClusterState struct { // XXX conflicts with ClusterState enum ...@@ -680,7 +628,6 @@ type X_ClusterState struct { // XXX conflicts with ClusterState enum
// If current_serial's data is current on storage. // If current_serial's data is current on storage.
// S -> C // S -> C
type ObjectUndoSerial struct { type ObjectUndoSerial struct {
PktHead
Tid Tid Tid Tid
LTID Tid LTID Tid
UndoneTID Tid UndoneTID Tid
...@@ -689,7 +636,6 @@ type ObjectUndoSerial struct { ...@@ -689,7 +636,6 @@ type ObjectUndoSerial struct {
// XXX answer_undo_transaction ? // XXX answer_undo_transaction ?
type AnswerObjectUndoSerial struct { type AnswerObjectUndoSerial struct {
PktHead
ObjectTIDDict map[Oid]struct { ObjectTIDDict map[Oid]struct {
CurrentSerial Tid CurrentSerial Tid
UndoSerial Tid UndoSerial Tid
...@@ -701,13 +647,11 @@ type AnswerObjectUndoSerial struct { ...@@ -701,13 +647,11 @@ type AnswerObjectUndoSerial struct {
// C -> S // C -> S
// Answer whether a transaction holds the write lock for requested object. // Answer whether a transaction holds the write lock for requested object.
type HasLock struct { type HasLock struct {
PktHead
Tid Tid Tid Tid
Oid Oid Oid Oid
} }
type AnswerHasLock struct { type AnswerHasLock struct {
PktHead
Oid Oid Oid Oid
LockState LockState LockState LockState
} }
...@@ -720,7 +664,6 @@ type AnswerHasLock struct { ...@@ -720,7 +664,6 @@ type AnswerHasLock struct {
// Same structure as AnswerStoreObject, to handle the same way, except there // Same structure as AnswerStoreObject, to handle the same way, except there
// is nothing to invalidate in any client's cache. // is nothing to invalidate in any client's cache.
type CheckCurrentSerial struct { type CheckCurrentSerial struct {
PktHead
Tid Tid Tid Tid
Serial Tid Serial Tid
Oid Oid Oid Oid
...@@ -740,12 +683,10 @@ type AnswerCheckCurrentSerial struct { ...@@ -740,12 +683,10 @@ type AnswerCheckCurrentSerial struct {
// S -> M // S -> M
// M -> C // M -> C
type Pack struct { type Pack struct {
PktHead
Tid Tid Tid Tid
} }
type AnswerPack struct { type AnswerPack struct {
PktHead
Status bool Status bool
} }
...@@ -753,7 +694,6 @@ type AnswerPack struct { ...@@ -753,7 +694,6 @@ type AnswerPack struct {
// ctl -> A // ctl -> A
// A -> M // A -> M
type CheckReplicas struct { type CheckReplicas struct {
PktHead
PartitionDict map[uint32/*PNumber*/]UUID // partition -> source PartitionDict map[uint32/*PNumber*/]UUID // partition -> source
MinTID Tid MinTID Tid
MaxTID Tid MaxTID Tid
...@@ -763,7 +703,6 @@ type CheckReplicas struct { ...@@ -763,7 +703,6 @@ type CheckReplicas struct {
// M -> S // M -> S
type CheckPartition struct { type CheckPartition struct {
PktHead
Partition uint32 // PNumber Partition uint32 // PNumber
Source struct { Source struct {
UpstreamName string UpstreamName string
...@@ -783,7 +722,6 @@ type CheckPartition struct { ...@@ -783,7 +722,6 @@ type CheckPartition struct {
// reference node. // reference node.
// S -> S // S -> S
type CheckTIDRange struct { type CheckTIDRange struct {
PktHead
Partition uint32 // PNumber Partition uint32 // PNumber
Length uint32 // PNumber Length uint32 // PNumber
MinTID Tid MinTID Tid
...@@ -791,7 +729,6 @@ type CheckTIDRange struct { ...@@ -791,7 +729,6 @@ type CheckTIDRange struct {
} }
type AnswerCheckTIDRange struct { type AnswerCheckTIDRange struct {
PktHead
Count uint32 // PNumber Count uint32 // PNumber
Checksum Checksum Checksum Checksum
MaxTID Tid MaxTID Tid
...@@ -806,7 +743,6 @@ type AnswerCheckTIDRange struct { ...@@ -806,7 +743,6 @@ type AnswerCheckTIDRange struct {
// reference node. // reference node.
// S -> S // S -> S
type CheckSerialRange struct { type CheckSerialRange struct {
PktHead
Partition uint32 // PNumber Partition uint32 // PNumber
Length uint32 // PNumber Length uint32 // PNumber
MinTID Tid MinTID Tid
...@@ -824,7 +760,6 @@ type AnswerCheckSerialRange struct { ...@@ -824,7 +760,6 @@ type AnswerCheckSerialRange struct {
// S -> M // S -> M
type PartitionCorrupted struct { type PartitionCorrupted struct {
PktHead
Partition uint32 // PNumber Partition uint32 // PNumber
CellList []UUID CellList []UUID
} }
...@@ -835,11 +770,9 @@ type PartitionCorrupted struct { ...@@ -835,11 +770,9 @@ type PartitionCorrupted struct {
// Answer last committed TID. // Answer last committed TID.
// M -> C // M -> C
type LastTransaction struct { type LastTransaction struct {
PktHead
} }
type AnswerLastTransaction struct { type AnswerLastTransaction struct {
PktHead
Tid Tid Tid Tid
} }
...@@ -847,7 +780,6 @@ type AnswerLastTransaction struct { ...@@ -847,7 +780,6 @@ type AnswerLastTransaction struct {
// Notify that node is ready to serve requests. // Notify that node is ready to serve requests.
// S -> M // S -> M
type NotifyReady struct { type NotifyReady struct {
PktHead
} }
// replication // replication
......
...@@ -22,19 +22,46 @@ import ( ...@@ -22,19 +22,46 @@ import (
"go/ast" "go/ast"
"go/parser" "go/parser"
"go/token" "go/token"
"go/types"
"log"
) )
var _ = ast.Print // information about one packet type
type PacketType struct {
name string
msgCode uint16 // message code for this packet type - derived from type order number in source
}
var fset = token.NewFileSet()
var info = &types.Info{
Types: make(map[ast.Expr]types.TypeAndValue),
Uses: make(map[*ast.Ident]types.Object),
Defs: make(map[*ast.Ident]types.Object),
}
// complete position of a node
func pos(n ast.Node) {
return fset.Position(n.Pos())
}
func main() { func main() {
fset := token.NewFileSet() typeMap := map[string]*PacketType{} // XXX needed ?
// go through proto.go and collect packets type definitions
var mode parser.Mode = 0 // parser.Trace var mode parser.Mode = 0 // parser.Trace
f, err := parser.ParseFile(fset, "proto.go", nil, mode) f, err := parser.ParseFile(fset, "proto.go", nil, mode)
if err != nil { if err != nil {
panic(err) // XXX log log.Fatal(err) // parse error
} }
conf := types.Config{}
pkg, err := conf.Check("proto", fset, []*ast.File{f}, info)
if err != nil {
log.Fatal(err) // typecheck error
}
ncode := 0 ncode := 0
//ast.Print(fset, f) //ast.Print(fset, f)
...@@ -42,53 +69,132 @@ func main() { ...@@ -42,53 +69,132 @@ func main() {
for _, decl := range f.Decls { for _, decl := range f.Decls {
// we look for types (which can be only under GenDecl) // we look for types (which can be only under GenDecl)
gdecl, ok := decl.(*ast.GenDecl) gendecl, ok := decl.(*ast.GenDecl)
if !ok || gdecl.Tok != token.TYPE { if !ok || gendecl.Tok != token.TYPE {
continue continue
} }
for _, spec := range gdecl.Specs { for _, spec := range gendecl.Specs {
tspec := spec.(*ast.TypeSpec) // must be because tok = TYPE typespec := spec.(*ast.TypeSpec) // must be because tok = TYPE
tname := tspec.Name.Name typename := typespec.Name.Name
// we are only interested in struct types switch t := typespec.Type.(type) {
tstruct, ok := tspec.Type.(*ast.StructType) default:
if !ok { // we are only interested in struct types
continue continue
}
//fmt.Printf("\n%s:\n", tname) case *ast.StructType:
//fmt.Println(tstruct) //fmt.Printf("\n%s:\n", typename)
//ast.Print(fset, tstruct) //fmt.Println(t)
//ast.Print(fset, t)
if ncode != 0 { PacketType{name: typename, msgCode: ncode}
fmt.Println()
}
for _, fieldv := range tstruct.Fields.List { // if ncode != 0 {
// we only support simple types like uint16 // fmt.Println()
ftype, ok := fieldv.Type.(*ast.Ident) // }
if !ok {
// TODO log
// TODO proper error message
panic(fmt.Sprintf("%#v not supported", fieldv.Type))
}
if len(fieldv.Names) != 0 { for _, fieldv := range t.Fields.List {
for _, field := range fieldv.Names { // we only support simple types like uint16
fmt.Printf("%s(%d).%s\t%s\n", tname, ncode, field.Name, ftype) ftype, ok := fieldv.Type.(*ast.Ident)
if !ok {
// TODO log
// TODO proper error message
panic(fmt.Sprintf("%#v not supported", fieldv.Type))
} }
} else {
// no names means embedding
fmt.Printf("%s(%d).<%s>\n", tname, ncode, ftype)
}
}
ncode++ if len(fieldv.Names) != 0 {
for _, field := range fieldv.Names {
fmt.Printf("%s(%d).%s\t%s\n", typename, ncode, field.Name, ftype)
}
} else {
// no names means embedding
fmt.Printf("%s(%d).<%s>\n", typename, ncode, ftype)
}
}
ncode++
}
} }
//fmt.Println(gdecl) //fmt.Println(gdecl)
//ast.Print(fset, gdecl) //ast.Print(fset, gdecl)
} }
} }
// wiresize returns wire size of a type
// type must be of fixed size (e.g. not a slice or map)
// XXX ast.Expr -> ?
func wiresize(*ast.Expr) int {
// TODO
}
func gendecode(typespec *ast.TypeSpec) string {
buf := butes.Buffer{}
typename := typespec.Name.Name
t := typespec.Type.(*ast.StructType) // must be
fmt.Fprintf(&buf, "func (p *%s) NEODecode(data []byte) int {\n", typename)
n := 0 // current decode pos in data
for _, fieldv := t.Fields.List {
// type B struct { ... }
//
// type A struct {
// x, y int <- fieldv
// B <- fieldv
// embedding: change `B` -> `B B` (field type must be Ident)
fieldnamev := fieldv.Names
if fieldnamev == nil {
fieldnamev = []*ast.Ident{fieldv.Type.(*ast.Ident)}
}
for fieldname := range fieldnamev {
switch fieldtype := fieldv.Type.(type) {
// we are processing: <fieldname> <fieldtype>
// simple types like uint16
case *ast.Ident:
// TODO
// array or slice
case *ast.ArrayType:
if fieldtype.Len != nil {
log.Fatalf("%s: TODO arrays not suported", pos(fieldtype))
}
eltsize := wiresize(fieldtype.Elt) // TODO
// len u32
// [len] items
emit("length = Uint32(data[%s:])", n)
n += 4
emit("for ; length != 0; length-- {")
emit("}")
// map
case *ast.MapType:
// len u32
// [len] key, value
emit("length = Uint32(data[%s:])", n)
n += 4
keysize := wiresize(fieldtype.Key)
valsize := wiresize(fieldtype.Value)
// XXX *ast.StructType ?
default:
panic() // TODO
}
}
}
fmt.Fprintf(&buf, "}\n")
// TODO format.Source(buf.Bytes()) (XXX -> better at top-level for whole file)
return buf.String()
}
...@@ -63,7 +63,7 @@ func (stor *StorageApplication) ServeLink(ctx context.Context, link *NodeLink) { ...@@ -63,7 +63,7 @@ func (stor *StorageApplication) ServeLink(ctx context.Context, link *NodeLink) {
switch pkt.MsgCode { switch pkt.MsgCode {
case GetObject: case GetObject:
req := GetObject{} req := GetObject{}
err = req.Decode(pkt.Payload()) err = req.NEODecode(pkt.Payload())
if err != nil { if err != nil {
sendErr("malformed GetObject packet:", err) sendErr("malformed GetObject packet:", err)
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment