github.com/matrixorigin/matrixone@v1.2.0/proto/txn.proto (about)

     1  /* 
     2   * Copyright 2021 Matrix Origin
     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  syntax = "proto3";
    18  package txn; 
    19  option go_package = "github.com/matrixorigin/matrixone/pkg/pb/txn";
    20  
    21  import "github.com/gogo/protobuf/gogoproto/gogo.proto";
    22  import "timestamp.proto";
    23  import "metadata.proto";
    24  import "lock.proto";
    25  
    26  option (gogoproto.goproto_enum_prefix_all) = true;
    27  
    28  // TxnIsolation txn txn isolation
    29  enum TxnIsolation {
    30      // SI snapshot isolation
    31      SI = 0;
    32      // RC read committed
    33      RC = 1;
    34  }
    35  
    36  // TxnMode txn mode
    37  enum TxnMode {
    38      // Optimistic check conflict on commit.
    39      Optimistic    = 0;
    40      // Pessimistic check conflict every write
    41      Pessimistic = 1;
    42  }
    43  
    44  // TxnStatus transaction status
    45  enum TxnStatus {
    46      // Active is the state of transaction creation, in this state, can execute the 
    47      // transaction Read/Write/Commit/Rollback.
    48      Active = 0;
    49      // Prepared for distributed transactions across TNs, a 2pc commit is performed, 
    50      // and the prepared status means that the transaction on a TN was executed 
    51      // successfully in the first phase. 
    52      // 
    53      // Note that this status needs to be saved to the LogService. Once the first 
    54      // phase of a transaction is successful, data cannot be lost.
    55      Prepared = 1;
    56      // Committing for distributed transactions across TNs, once all TNs have completed
    57      // the first phase, the transaction enters the Committing state and initiates an 
    58      // asynchronous process to handle the commit of temporary data.
    59      //
    60      // Note that when all TNs involved are in the prepared state, the distributed 
    61      // transaction can be considered committed because all data has been written 
    62      // successfully. The subsequent Committing process just explicitly converts these
    63      // writes into committed data.
    64      //
    65      // Note that the state exists only in memory and is not persisted to the LogService.
    66      // It can be restored through the Prepared state, if all(TN).Status == Prepared.
    67      Committing = 2;
    68      // Committed after the Committing phase has transformed all TN data involved into 
    69      // committed data, the status of the distributed transaction is explicitly recorded 
    70      // as Committed.
    71      //
    72      // Note that this status needs to be saved to the LogService
    73      Committed = 3;
    74      // Aborting a client initiating a Rollback call or a distributed transaction that has
    75      // any error in the first phase will enter the Aborting state. This state starts an 
    76      // asynchronous task to clean up the temporary data written by the transaction.
    77      //
    78      // Note that the state exists only in memory and is not persisted to the LogService.
    79      // It can be restored through the Prepared state, if Any(TN).Status != Prepared.
    80      Aborting  = 4;
    81      // Aborted after the Aborting phase, all data involved in the TN is cleaned up and 
    82      // the transaction status is explicitly recorded as Aborted.
    83      //
    84      // Note that this status needs to be saved to the LogService
    85      Aborted   = 5;
    86  }
    87  
    88  // TxnMeta transaction metadata
    89  message TxnMeta {
    90      // ID transaction id, generated at the CN node at the time of transaction creation, 
    91      // globally unique.
    92      bytes                     ID           = 1;
    93      // Status transaction status
    94      TxnStatus                 Status       = 2;
    95      // SnapshotTS transaction read timestamp, generated at the CN node at the time of 
    96      // transaction creation. All data.TS < txn.SnapshotTS is visible for the current 
    97      // transaction.
    98      timestamp.Timestamp       SnapshotTS   = 3 [(gogoproto.nullable) = false];
    99      // PreparedTS timestamp to complete the first phase of a 2pc commit transaction.
   100      timestamp.Timestamp       PreparedTS   = 4 [(gogoproto.nullable) = false];
   101      // CommitTS transaction commit timestamp. For a 2pc transaction, commitTS = max(preparedTS).
   102      timestamp.Timestamp       CommitTS     = 5 [(gogoproto.nullable) = false];
   103      // TNShards all TNShards that have written data. The first TN is the coordinator of the 
   104      // transaction
   105      repeated metadata.TNShard TNShards     = 6 [(gogoproto.nullable) = false];
   106      // LockTables For pessimistic transactions, LockTables record the bind metadata of the 
   107      // LockTable corresponding to the successful locking of the current transaction. This data 
   108      // is committed to the TN at Commit time, and the TN will check once if these bindings have 
   109      // changed, and if they have, the transaction will be rolled back.
   110      repeated lock.LockTable   LockTables   = 7 [(gogoproto.nullable) = false];
   111      // TxnMode txn mode
   112      TxnMode                   Mode         = 8;
   113      // TxnIsolation isolation
   114      TxnIsolation              Isolation    = 9;
   115      // Mirror is mirror is true, means the current txn is not created on current node.
   116      bool                      Mirror       = 10;
   117      // LockService lock service's service address. Empty if is not pessimistic txn.
   118      string                    LockService  = 11;
   119  }
   120  
   121  // CNTxnSnapshot snapshot of the cn txn operation.
   122  message CNTxnSnapshot {
   123      // ID txn id
   124      TxnMeta                   Txn              = 1 [(gogoproto.nullable) = false];
   125      // Deprecated: use TxnOptions
   126      bool                      ReadyOnly        = 2;
   127      // Deprecated: use TxnOptions
   128      bool                      EnableCacheWrite = 3;
   129      // Deprecated: use TxnOptions
   130      bool                      Disable1PCOpt    = 4;
   131      repeated lock.LockTable   LockTables       = 5 [(gogoproto.nullable) = false];
   132      TxnOptions                Options          = 6 [(gogoproto.nullable) = false];
   133  }
   134  
   135  // CNOpRequest cn read/write request, CN -> TN. If data is written to more than one TN (>1) in a 
   136  // single transaction, then the transaction becomes a 2pc transaction.
   137  message CNOpRequest {
   138      // OpCode request operation type
   139      uint32           OpCode  = 1;
   140      // Payload the content of the request, TxnClient does not perceive the exact
   141      // format and content
   142      bytes            Payload = 2;
   143      // Target target to which the request was sent
   144  	metadata.TNShard Target  = 3 [(gogoproto.nullable) = false]; 
   145  }
   146  
   147  // CNOpResponse cn read/write response, TN -> CN. A request corresponds to a response.
   148  message CNOpResponse {
   149      // Payload response payload
   150  	bytes Payload = 1;
   151  }
   152  
   153  // TxnMethod transaction operations
   154  enum TxnMethod {
   155      // Read transaction read
   156      Read              = 0;
   157      // Write transaction write
   158      Write             = 1;
   159      // Commit commit transaction
   160      Commit            = 2;
   161      // Rollback rollback transaction
   162      Rollback          = 3;
   163      // Prepare when TN(Coordinator) receives a commit request from CN, it sends a prepare to 
   164      // each TN(TNShard)
   165      Prepare           = 4;
   166      // GetStatus query the status of a transaction on a TN. When a TN encounters a transaction
   167      // in the Prepared state, it needs to go to the TN(Coordinator) to query the status of the 
   168      // current transaction. When a TN encounters a transaction in the Prepared state during the 
   169      // recover, it needs to query the status of the transaction on each TN(TNShard) to determine
   170      // if the transaction is committed.
   171      GetStatus         = 5;
   172      // CommitTNShard after the 2pc transaction is committed, the temporary data on each TN needs
   173      // to be explicitly converted to committed data.
   174      CommitTNShard     = 6;
   175      // RollbackTNShard after the 2pc transaction is aborted, the temporary data on each TN needs
   176      // to cleanup.
   177      RollbackTNShard   = 7;
   178      // RemoveMedata Remove metadata for transactions on TNShard. For a 2pc distributed transaction, 
   179      // after all participating TNShards have Prepared successfully, the asynchronous commit process 
   180      // starts, sending CommitTNShard requests to all participating TNShards in parallel. After each 
   181      // TNShard has processed the CommitTNShard, the metadata of the transaction cannot be deleted 
   182      // immediately, otherwise when the transaction coordinator node is down and restarted, the commit 
   183      // status of the transaction cannot be determined in the recovery process, as it is possible that
   184      // some participating TNShards cannot find the transaction information.
   185      //
   186      // TODO: needs to work with TAE's log compaction, not currently supported.
   187      RemoveMedata      = 8;
   188      // DEBUG used to send debug request from cn to tn, and received response from tn to cn
   189      DEBUG             = 9;
   190  }
   191  
   192  // TxnRequest transaction request. All requests for the transaction are made using TxnRequest, so that 
   193  // the codec and logical processing of the RPC can be unified. Specific requests are selected according 
   194  // to TxnMethod.
   195  //
   196  // Request flow of TxnRequest as below:
   197  // 1. CN -> TN (TxnMethod.Read, TxnMethod.Write, TxnMethod.Commit, TxnMethod.Rollback)
   198  // 2. TN -> TN (TxnMethod.Prepare, TxnMethod.GetStatus, TxnMethod.CommitTNShard, TxnMethod.RollbackTNShard,
   199  //             TxnMethod.RemoveMetadata)
   200  message TxnRequest {
   201      // RequestID request id
   202      uint64                      RequestID                = 1;
   203      // Txn transaction metadata
   204      TxnMeta                     Txn                      = 2 [(gogoproto.nullable) = false];
   205      // TxnMethod TxnRequest opCode, select the Request defined below according to TxnMethod.
   206      TxnMethod                   Method                   = 3;
   207      // Flag request flag
   208      uint32                      Flag                     = 4;
   209      // CNOpRequest corresponds to TxnMethod.Read, TxnMethod.Write
   210      CNOpRequest                 CNRequest                = 5; 
   211      // TxnCommitRequest corresponds to TxnMethod.Commit
   212      TxnCommitRequest            CommitRequest            = 6;
   213      // TxnRollbackRequest corresponds to TxnMethod.Rollback
   214      TxnRollbackRequest          RollbackRequest          = 7;
   215      // TxnPrepareRequest corresponds to TxnMethod.Prepare
   216      TxnPrepareRequest           PrepareRequest           = 8;
   217      // TxnGetStatusRequest corresponds to TxnMethod.GetStatus
   218      TxnGetStatusRequest         GetStatusRequest         = 9;
   219      // TxnCommitTNShardRequest corresponds to TxnMethod.CommitTNShard
   220      TxnCommitTNShardRequest     CommitTNShardRequest     = 10;
   221      // TxnRollbackTNShardRequest corresponds to TxnMethod.RollbackTNShard
   222      TxnRollbackTNShardRequest   RollbackTNShardRequest   = 11;
   223      // TxnRemoveMetadataRequest  corresponds to TxnMethod.RemoveMetadata
   224      TxnRemoveMetadataRequest    RemoveMetadata           = 12;
   225      // TxnRequestOptions request options
   226      TxnRequestOptions           Options                  = 13;
   227  }
   228  
   229  // TxnRequestOptions txn options
   230  message TxnRequestOptions {
   231      // RetryCodes when TN processes TxnRequest and encounters the specified error, it needs to retry
   232      // on the server side. Only read and write can retry.
   233      repeated int32 RetryCodes    = 1;
   234      // RetryInterval retry interval, default is 100ms.
   235      int64              RetryInterval = 2;    
   236  }
   237  
   238  // TxnResponse response of TxnRequest.
   239  message TxnResponse {
   240      // RequestID corresponding request id
   241      uint64                       RequestID                 = 1;
   242      // Txn transaction metadata. TxnResponse.TxnMeta and TxnRequest.TxnMeta may differ 
   243      // in that the node initiating the TxnRequest needs to process the returned TxnMeta, 
   244      // e.g. to determine whether the transaction is Aborted by the status of the returned 
   245      // TxnMeta.
   246      TxnMeta                      Txn                       = 2;
   247      // TxnMethod same as TxnRequest.TxnMethod
   248      TxnMethod                    Method                    = 3;
   249      // Flag request flag, same as the corresponding request
   250      uint32                       Flag                      = 4;
   251      // TxnError explicit error
   252      TxnError                     TxnError                  = 5;
   253      // CNOpResponse corresponds to TxnMethod.Read, TxnMethod.Write response
   254      CNOpResponse                 CNOpResponse              = 6; 
   255      // TxnCommitResponse corresponds to TxnMethod.Commit response
   256      TxnCommitResponse            CommitResponse            = 7;
   257      // TxnRollbackResponse corresponds to TxnMethod.Rollback response
   258      TxnRollbackResponse          RollbackResponse          = 8;
   259      // TxnPrepareResponse corresponds to TxnMethod.Prepare response
   260      TxnPrepareResponse           PrepareResponse           = 9;
   261      // TxnGetStatusResponse corresponds to TxnMethod.GetStatus response
   262      TxnGetStatusResponse         GetStatusResponse         = 10;
   263      // TxnCommitTNShardResponse corresponds to TxnMethod.CommitTNShard response
   264      TxnCommitTNShardResponse     CommitTNShardResponse     = 11;
   265      // TxnRollbackTNShardResponse corresponds to TxnMethod.RollbackTNShard response
   266      TxnRollbackTNShardResponse   RollbackTNShardResponse   = 12;
   267       // TxnRemoveMetadataResponse  corresponds to TxnMethod.RemoveMetadata
   268       TxnRemoveMetadataResponse   RemoveMetadata            = 13;
   269  }
   270  
   271  // TxnCommitRequest CN sent the commit request to coordinator TN.
   272  message TxnCommitRequest {
   273      repeated TxnRequest       Payload = 1;
   274      bool                      Disable1PCOpt = 2;
   275  }
   276  
   277  // TxnCommitResponse response of TxnCommitRequest. 
   278  message TxnCommitResponse {
   279      repeated uint64 InvalidLockTables = 1;
   280  }
   281  
   282  // TxnCommitRequest CN sent the rollback request to coordinator TN.
   283  message TxnRollbackRequest {
   284  }
   285  
   286  // TxnRollbackResponse response of TxnRollbackRequest.
   287  message TxnRollbackResponse {
   288  }
   289  
   290  // TxnPrepareRequest when a TN(coordinator) receives a Commit request from a CN, if 
   291  // more than one TN is involved, the 2PC commit process is enabled and the first phase
   292  // is to send prepare requests to all TNs.
   293  message TxnPrepareRequest {
   294      // TNShard prepare TN
   295      metadata.TNShard          TNShard  = 1 [(gogoproto.nullable) = false];
   296  }
   297  
   298  // TxnPrepareResponse response of TxnPrepareRequest
   299  message TxnPrepareResponse {
   300  
   301  }
   302  
   303  // TxnGetStatusRequest query the status of a transaction on TN
   304  message TxnGetStatusRequest {
   305      // TNShard target TN
   306      metadata.TNShard TNShard = 1 [(gogoproto.nullable) = false];
   307  }
   308  
   309  // TxnGetStatusResponse response of TxnGetStatusRequest
   310  message TxnGetStatusResponse {
   311  }
   312  
   313  // TxnCommitTNShardRequest commit txn on TNShard. Data needs to be written to the 
   314  // LogService.
   315  message TxnCommitTNShardRequest {
   316       // TNShard target TN
   317       metadata.TNShard TNShard = 1 [(gogoproto.nullable) = false];
   318  }
   319  
   320  // TxnCommitTNShardResponse response of TxnCommitTNShardRequest
   321  message TxnCommitTNShardResponse {
   322  }
   323  
   324  // TxnRollbackTNShardRequest rollback txn on TNShard
   325  message TxnRollbackTNShardRequest {
   326       // TNShard target TN
   327       metadata.TNShard TNShard = 1 [(gogoproto.nullable) = false];
   328  }
   329  
   330  // TxnRollbackTNShardResponse response of TxnRollbackTNShardRequest
   331  message TxnRollbackTNShardResponse {
   332  }
   333  
   334  
   335  // TxnRemoveMetadataRequest remove txn metadata on TNShard
   336  message TxnRemoveMetadataRequest {
   337      // TNShard target TN
   338      metadata.TNShard TNShard = 1 [(gogoproto.nullable) = false];
   339  }
   340  
   341  // TxnRemoveMetadataResponse response of TxnRemoveMetadataRequest
   342  message TxnRemoveMetadataResponse {
   343  }
   344  
   345  // TxnError all explicit errors in transaction operations. 
   346  message TxnError {
   347      // Code moerr code, used to special error handle without unmarshal moerr
   348      uint32 Code       = 1;
   349      // Error we use this field to send moerr from tn to cn. Set with 
   350      // moerr.MarshalBinary, and use moerr.UnmarshalBinary to restore 
   351      // moerr.
   352      bytes  Error      = 2;
   353      // TxnErrCode is a internal err code in the txn framework and used to indicate
   354      // what transaction operation failed. Usually this value is the same as the value 
   355      // of Code, except for the error returned by the interface call to TxnStorage.
   356      // Because the types of errors returned by TxnStorage's interface are unknown to the 
   357      // transaction framework, it is necessary to use a code for the interface call to uniformly
   358      // replace these error codes.
   359      uint32 TxnErrCode = 3;
   360  }
   361  
   362  message TxnOptions {
   363      uint32                 Features           = 1;
   364      string                 CN                 = 2;
   365      string                 SessionID          = 3;
   366      uint32                 AccountID          = 4;
   367      uint32                 ConnectionID       = 5;
   368      string                 UserName           = 6;
   369      repeated uint64        SkipLockTables     = 7;
   370      repeated lock.LockMode SkipLockTableModes = 8;
   371      string counter = 9;
   372      string sessionInfo = 10;
   373      bool inRunSql = 11;
   374      bool inCommit = 12;
   375      bool inRollback = 13;
   376  }