go.temporal.io/server@v1.23.0/common/persistence/sql/sqlplugin/postgresql/shard.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package postgresql
    26  
    27  import (
    28  	"context"
    29  	"database/sql"
    30  
    31  	"go.temporal.io/server/common/persistence/sql/sqlplugin"
    32  )
    33  
    34  const (
    35  	createShardQry = `INSERT INTO
    36   shards (shard_id, range_id, data, data_encoding) VALUES ($1, $2, $3, $4)`
    37  
    38  	getShardQry = `SELECT
    39   shard_id, range_id, data, data_encoding
    40   FROM shards WHERE shard_id = $1`
    41  
    42  	updateShardQry = `UPDATE shards 
    43   SET range_id = $1, data = $2, data_encoding = $3 
    44   WHERE shard_id = $4`
    45  
    46  	lockShardQry     = `SELECT range_id FROM shards WHERE shard_id = $1 FOR UPDATE`
    47  	readLockShardQry = `SELECT range_id FROM shards WHERE shard_id = $1 FOR SHARE`
    48  )
    49  
    50  // InsertIntoShards inserts one or more rows into shards table
    51  func (pdb *db) InsertIntoShards(
    52  	ctx context.Context,
    53  	row *sqlplugin.ShardsRow,
    54  ) (sql.Result, error) {
    55  	return pdb.conn.ExecContext(ctx,
    56  		createShardQry,
    57  		row.ShardID,
    58  		row.RangeID,
    59  		row.Data,
    60  		row.DataEncoding,
    61  	)
    62  }
    63  
    64  // UpdateShards updates one or more rows into shards table
    65  func (pdb *db) UpdateShards(
    66  	ctx context.Context,
    67  	row *sqlplugin.ShardsRow,
    68  ) (sql.Result, error) {
    69  	return pdb.conn.ExecContext(ctx,
    70  		updateShardQry,
    71  		row.RangeID,
    72  		row.Data,
    73  		row.DataEncoding,
    74  		row.ShardID,
    75  	)
    76  }
    77  
    78  // SelectFromShards reads one or more rows from shards table
    79  func (pdb *db) SelectFromShards(
    80  	ctx context.Context,
    81  	filter sqlplugin.ShardsFilter,
    82  ) (*sqlplugin.ShardsRow, error) {
    83  	var row sqlplugin.ShardsRow
    84  	err := pdb.conn.GetContext(ctx,
    85  		&row,
    86  		getShardQry,
    87  		filter.ShardID,
    88  	)
    89  	if err != nil {
    90  		return nil, err
    91  	}
    92  	return &row, nil
    93  }
    94  
    95  // ReadLockShards acquires a read lock on a single row in shards table
    96  func (pdb *db) ReadLockShards(
    97  	ctx context.Context,
    98  	filter sqlplugin.ShardsFilter,
    99  ) (int64, error) {
   100  	var rangeID int64
   101  	err := pdb.conn.GetContext(ctx,
   102  		&rangeID,
   103  		readLockShardQry,
   104  		filter.ShardID,
   105  	)
   106  	return rangeID, err
   107  }
   108  
   109  // WriteLockShards acquires a write lock on a single row in shards table
   110  func (pdb *db) WriteLockShards(
   111  	ctx context.Context,
   112  	filter sqlplugin.ShardsFilter,
   113  ) (int64, error) {
   114  	var rangeID int64
   115  	err := pdb.conn.GetContext(ctx,
   116  		&rangeID,
   117  		lockShardQry,
   118  		filter.ShardID,
   119  	)
   120  	return rangeID, err
   121  }