Files
Alex ShtinandGitHub 8e8f1b0f7c Remove license header from every file (#7689)
## What changed?
<!-- Describe what has changed in this PR -->
Remove license header from every file. Because it is really hard to
follow in this PR here is the summary:
1. License header is removed from all `.go` and `.proto` files
:fireworks::fireworks:🎆.
2. `LICENSE` file in the root directory has only Temporal and Uber
copyrights.
3. 5 other `LICENSE` files added to the packages which have copyrights
different from Temporal and Uber: Datadog, Xargin, "Mat Ryer, Tyler
Bunnell and contributors".
4. `license_file` flag is removed from all code generation tools.
5. `copyright_file` flag is removed from `go:generate mockgen`
directive.
6. All copyright related targets are removed from `Makefile`.
7. Updated Temporal copyright year to 2025 everywhere.

## Why?
<!-- Tell your future self why have you made these changes -->
I double checked with legal department that it is not needed to have
license header in every file. One file per repo is enough. I put all
copyrights to the root `LICENSE` file and removed header from all other
files. Also updated tools and `Makefile`.
2025-05-01 18:50:21 -07:00

187 lines
4.2 KiB
Go

package gocql
import (
"context"
"sync"
"sync/atomic"
"time"
"github.com/gocql/gocql"
"go.temporal.io/server/common"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/log/tag"
"go.temporal.io/server/common/metrics"
)
var _ Session = (*session)(nil)
const (
sessionRefreshMinInternal = 5 * time.Second
)
const (
refreshThrottleTagValue = "throttle"
refreshErrorTagValue = "error"
)
type (
session struct {
status int32
newClusterConfigFunc func() (*gocql.ClusterConfig, error)
atomic.Value // *gocql.Session
logger log.Logger
sync.Mutex
sessionInitTime time.Time
metricsHandler metrics.Handler
}
)
func NewSession(
newClusterConfigFunc func() (*gocql.ClusterConfig, error),
logger log.Logger,
metricsHandler metrics.Handler,
) (*session, error) {
gocqlSession, err := initSession(logger, newClusterConfigFunc, metricsHandler)
if err != nil {
return nil, err
}
session := &session{
status: common.DaemonStatusStarted,
newClusterConfigFunc: newClusterConfigFunc,
logger: logger,
metricsHandler: metricsHandler,
sessionInitTime: time.Now().UTC(),
}
session.Value.Store(gocqlSession)
return session, nil
}
func (s *session) refresh() {
if atomic.LoadInt32(&s.status) != common.DaemonStatusStarted {
return
}
s.Lock()
defer s.Unlock()
if time.Now().UTC().Sub(s.sessionInitTime) < sessionRefreshMinInternal {
s.logger.Warn("gocql wrapper: did not refresh gocql session because the last refresh was too close",
tag.NewDurationTag("min_refresh_interval_seconds", sessionRefreshMinInternal))
handler := s.metricsHandler.WithTags(metrics.FailureTag(refreshThrottleTagValue))
metrics.CassandraSessionRefreshFailures.With(handler).Record(1)
return
}
newSession, err := initSession(s.logger, s.newClusterConfigFunc, s.metricsHandler)
if err != nil {
s.logger.Error("gocql wrapper: unable to refresh gocql session", tag.Error(err))
handler := s.metricsHandler.WithTags(metrics.FailureTag(refreshErrorTagValue))
metrics.CassandraSessionRefreshFailures.With(handler).Record(1)
return
}
s.sessionInitTime = time.Now().UTC()
oldSession := s.Value.Load().(*gocql.Session)
s.Value.Store(newSession)
go oldSession.Close()
s.logger.Warn("gocql wrapper: successfully refreshed gocql session")
}
func initSession(
logger log.Logger,
newClusterConfigFunc func() (*gocql.ClusterConfig, error),
metricsHandler metrics.Handler,
) (gs *gocql.Session, retErr error) {
defer log.CapturePanic(logger, &retErr)
cluster, err := newClusterConfigFunc()
if err != nil {
return nil, err
}
start := time.Now()
defer func() {
metrics.CassandraInitSessionLatency.With(metricsHandler).Record(time.Since(start))
}()
return cluster.CreateSession()
}
func (s *session) Query(
stmt string,
values ...interface{},
) Query {
q := s.Value.Load().(*gocql.Session).Query(stmt, values...)
if q == nil {
return nil
}
return &query{
session: s,
gocqlQuery: q,
}
}
func (s *session) NewBatch(
batchType BatchType,
) *Batch {
b := s.Value.Load().(*gocql.Session).NewBatch(mustConvertBatchType(batchType))
if b == nil {
return nil
}
return &Batch{
session: s,
gocqlBatch: b,
}
}
func (s *session) ExecuteBatch(
b *Batch,
) (retError error) {
defer func() { s.handleError(retError) }()
return s.Value.Load().(*gocql.Session).ExecuteBatch(b.gocqlBatch)
}
func (s *session) MapExecuteBatchCAS(
b *Batch,
previous map[string]interface{},
) (_ bool, _ Iter, retError error) {
defer func() { s.handleError(retError) }()
applied, iter, err := s.Value.Load().(*gocql.Session).MapExecuteBatchCAS(b.gocqlBatch, previous)
return applied, iter, err
}
func (s *session) AwaitSchemaAgreement(
ctx context.Context,
) (retError error) {
defer func() { s.handleError(retError) }()
return s.Value.Load().(*gocql.Session).AwaitSchemaAgreement(ctx)
}
func (s *session) Close() {
if !atomic.CompareAndSwapInt32(
&s.status,
common.DaemonStatusStarted,
common.DaemonStatusStopped,
) {
return
}
s.Value.Load().(*gocql.Session).Close()
}
func (s *session) handleError(
err error,
) {
switch err {
case gocql.ErrNoConnections,
gocql.ErrSessionClosed:
s.refresh()
default:
// noop
}
}