Initial commit: Open sourcing all of the Maple Open Technologies code.
This commit is contained in:
commit
755d54a99d
2010 changed files with 448675 additions and 0 deletions
|
|
@ -0,0 +1,159 @@
|
|||
// File Path: monorepo/cloud/maplefile-backend/pkg/storage/database/cassandradb/cassandradb.go
|
||||
package cassandradb
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gocql/gocql"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/config"
|
||||
)
|
||||
|
||||
// CassandraDB wraps the gocql session with additional functionality
|
||||
type CassandraDB struct {
|
||||
Session *gocql.Session
|
||||
config config.DatabaseConfig
|
||||
}
|
||||
|
||||
// gocqlLogger wraps zap logger to filter out noisy gocql warnings
|
||||
type gocqlLogger struct {
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
// Print implements gocql's Logger interface
|
||||
func (l *gocqlLogger) Print(v ...interface{}) {
|
||||
msg := fmt.Sprint(v...)
|
||||
|
||||
// Filter out noisy "invalid peer" warnings from Cassandra gossip
|
||||
// These are harmless and occur due to Docker networking
|
||||
if strings.Contains(msg, "Found invalid peer") {
|
||||
return
|
||||
}
|
||||
|
||||
// Log other messages at debug level
|
||||
l.logger.Debug(msg)
|
||||
}
|
||||
|
||||
// Printf implements gocql's Logger interface
|
||||
func (l *gocqlLogger) Printf(format string, v ...interface{}) {
|
||||
msg := fmt.Sprintf(format, v...)
|
||||
|
||||
// Filter out noisy "invalid peer" warnings from Cassandra gossip
|
||||
if strings.Contains(msg, "Found invalid peer") {
|
||||
return
|
||||
}
|
||||
|
||||
// Log other messages at debug level
|
||||
l.logger.Debug(msg)
|
||||
}
|
||||
|
||||
// Println implements gocql's Logger interface
|
||||
func (l *gocqlLogger) Println(v ...interface{}) {
|
||||
msg := fmt.Sprintln(v...)
|
||||
|
||||
// Filter out noisy "invalid peer" warnings from Cassandra gossip
|
||||
if strings.Contains(msg, "Found invalid peer") {
|
||||
return
|
||||
}
|
||||
|
||||
// Log other messages at debug level
|
||||
l.logger.Debug(msg)
|
||||
}
|
||||
|
||||
// NewCassandraConnection establishes a connection to Cassandra cluster
|
||||
// Uses the simplified approach from MaplePress (working code)
|
||||
func NewCassandraConnection(cfg *config.Config, logger *zap.Logger) (*gocql.Session, error) {
|
||||
dbConfig := cfg.Database
|
||||
|
||||
logger.Info("⏳ Connecting to Cassandra...",
|
||||
zap.Strings("hosts", dbConfig.Hosts),
|
||||
zap.String("keyspace", dbConfig.Keyspace))
|
||||
|
||||
// Create cluster configuration - let gocql handle DNS resolution
|
||||
cluster := gocql.NewCluster(dbConfig.Hosts...)
|
||||
cluster.Keyspace = dbConfig.Keyspace
|
||||
cluster.Consistency = parseConsistency(dbConfig.Consistency)
|
||||
cluster.ProtoVersion = 4
|
||||
cluster.ConnectTimeout = dbConfig.ConnectTimeout
|
||||
cluster.Timeout = dbConfig.RequestTimeout
|
||||
cluster.NumConns = 2
|
||||
|
||||
// Set custom logger to filter out noisy warnings
|
||||
cluster.Logger = &gocqlLogger{logger: logger.Named("gocql")}
|
||||
|
||||
// Retry policy
|
||||
cluster.RetryPolicy = &gocql.ExponentialBackoffRetryPolicy{
|
||||
NumRetries: int(dbConfig.MaxRetryAttempts),
|
||||
Min: dbConfig.RetryDelay,
|
||||
Max: 10 * time.Second,
|
||||
}
|
||||
|
||||
// Enable compression for better network efficiency
|
||||
cluster.Compressor = &gocql.SnappyCompressor{}
|
||||
|
||||
// Create session
|
||||
session, err := cluster.CreateSession()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to connect to Cassandra: %w", err)
|
||||
}
|
||||
|
||||
logger.Info("✓ Cassandra connected",
|
||||
zap.String("consistency", dbConfig.Consistency),
|
||||
zap.Int("connections", cluster.NumConns))
|
||||
|
||||
return session, nil
|
||||
}
|
||||
|
||||
// Close terminates the database connection
|
||||
func (db *CassandraDB) Close() {
|
||||
if db.Session != nil {
|
||||
db.Session.Close()
|
||||
}
|
||||
}
|
||||
|
||||
// Health checks if the database connection is still alive
|
||||
func (db *CassandraDB) Health() error {
|
||||
// Quick health check using a simple query
|
||||
var timestamp time.Time
|
||||
err := db.Session.Query("SELECT now() FROM system.local").Scan(×tamp)
|
||||
if err != nil {
|
||||
return fmt.Errorf("health check failed: %w", err)
|
||||
}
|
||||
|
||||
// Validate that we got a reasonable timestamp (within last minute)
|
||||
now := time.Now()
|
||||
if timestamp.Before(now.Add(-time.Minute)) || timestamp.After(now.Add(time.Minute)) {
|
||||
return fmt.Errorf("health check returned suspicious timestamp: %v (current: %v)", timestamp, now)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// parseConsistency converts string consistency level to gocql.Consistency
|
||||
func parseConsistency(consistency string) gocql.Consistency {
|
||||
switch consistency {
|
||||
case "ANY":
|
||||
return gocql.Any
|
||||
case "ONE":
|
||||
return gocql.One
|
||||
case "TWO":
|
||||
return gocql.Two
|
||||
case "THREE":
|
||||
return gocql.Three
|
||||
case "QUORUM":
|
||||
return gocql.Quorum
|
||||
case "ALL":
|
||||
return gocql.All
|
||||
case "LOCAL_QUORUM":
|
||||
return gocql.LocalQuorum
|
||||
case "EACH_QUORUM":
|
||||
return gocql.EachQuorum
|
||||
case "LOCAL_ONE":
|
||||
return gocql.LocalOne
|
||||
default:
|
||||
return gocql.Quorum // Default to QUORUM
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,146 @@
|
|||
// File Path: monorepo/cloud/maplefile-backend/pkg/storage/database/cassandradb/migration.go
|
||||
package cassandradb
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/golang-migrate/migrate/v4"
|
||||
_ "github.com/golang-migrate/migrate/v4/database/cassandra"
|
||||
_ "github.com/golang-migrate/migrate/v4/source/file"
|
||||
|
||||
"codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/config"
|
||||
)
|
||||
|
||||
// Migrator handles database schema migrations
|
||||
// This encapsulates all migration logic and makes it testable
|
||||
type Migrator struct {
|
||||
config config.DatabaseConfig
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
// NewMigrator creates a new migration manager that works with fx dependency injection
|
||||
func NewMigrator(cfg *config.Configuration, logger *zap.Logger) *Migrator {
|
||||
return &Migrator{
|
||||
config: cfg.Database,
|
||||
logger: logger.Named("Migrator"),
|
||||
}
|
||||
}
|
||||
|
||||
// Up runs all pending migrations with dirty state recovery
|
||||
func (m *Migrator) Up() error {
|
||||
m.logger.Info("Creating migrator")
|
||||
migrateInstance, err := m.createMigrate()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create migrator: %w", err)
|
||||
}
|
||||
defer migrateInstance.Close()
|
||||
|
||||
m.logger.Info("Checking migration version")
|
||||
version, dirty, err := migrateInstance.Version()
|
||||
if err != nil && err != migrate.ErrNilVersion {
|
||||
return fmt.Errorf("failed to get migration version: %w", err)
|
||||
}
|
||||
|
||||
if dirty {
|
||||
m.logger.Warn("Database is in dirty state, attempting to force clean state",
|
||||
zap.Uint("version", version))
|
||||
if err := migrateInstance.Force(int(version)); err != nil {
|
||||
return fmt.Errorf("failed to force clean migration state: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Run migrations
|
||||
if err := migrateInstance.Up(); err != nil && err != migrate.ErrNoChange {
|
||||
return fmt.Errorf("failed to run migrations: %w", err)
|
||||
}
|
||||
|
||||
// Get final version
|
||||
finalVersion, _, err := migrateInstance.Version()
|
||||
if err != nil && err != migrate.ErrNilVersion {
|
||||
m.logger.Warn("Could not get final migration version",
|
||||
zap.Error(err))
|
||||
} else if err != migrate.ErrNilVersion {
|
||||
m.logger.Info("Database migrations completed successfully",
|
||||
zap.Uint("version", finalVersion))
|
||||
} else {
|
||||
m.logger.Info("Database migrations completed successfully (no migrations applied)")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Down rolls back the last migration
|
||||
// Useful for development and rollback scenarios
|
||||
func (m *Migrator) Down() error {
|
||||
migrate, err := m.createMigrate()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create migrator: %w", err)
|
||||
}
|
||||
defer migrate.Close()
|
||||
|
||||
if err := migrate.Steps(-1); err != nil {
|
||||
return fmt.Errorf("failed to rollback migration: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Version returns the current migration version
|
||||
func (m *Migrator) Version() (uint, bool, error) {
|
||||
migrate, err := m.createMigrate()
|
||||
if err != nil {
|
||||
return 0, false, fmt.Errorf("failed to create migrator: %w", err)
|
||||
}
|
||||
defer migrate.Close()
|
||||
|
||||
return migrate.Version()
|
||||
}
|
||||
|
||||
// ForceVersion forces the migration version (useful for fixing dirty states)
|
||||
func (m *Migrator) ForceVersion(version int) error {
|
||||
migrateInstance, err := m.createMigrate()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create migrator: %w", err)
|
||||
}
|
||||
defer migrateInstance.Close()
|
||||
|
||||
if err := migrateInstance.Force(version); err != nil {
|
||||
return fmt.Errorf("failed to force version %d: %w", version, err)
|
||||
}
|
||||
|
||||
m.logger.Info("Successfully forced migration version",
|
||||
zap.Int("version", version))
|
||||
return nil
|
||||
}
|
||||
|
||||
// createMigrate creates a migrate instance with proper configuration
|
||||
func (m *Migrator) createMigrate() (*migrate.Migrate, error) {
|
||||
// Build Cassandra connection string
|
||||
// Format: cassandra://host:port/keyspace?consistency=level
|
||||
databaseURL := fmt.Sprintf("cassandra://%s/%s?consistency=%s",
|
||||
m.config.Hosts[0], // Use first host for migrations
|
||||
m.config.Keyspace,
|
||||
m.config.Consistency,
|
||||
)
|
||||
|
||||
// Add authentication if configured
|
||||
if m.config.Username != "" && m.config.Password != "" {
|
||||
databaseURL = fmt.Sprintf("cassandra://%s:%s@%s/%s?consistency=%s",
|
||||
m.config.Username,
|
||||
m.config.Password,
|
||||
m.config.Hosts[0],
|
||||
m.config.Keyspace,
|
||||
m.config.Consistency,
|
||||
)
|
||||
}
|
||||
|
||||
// Create migrate instance
|
||||
migrate, err := migrate.New(m.config.MigrationsPath, databaseURL)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to initialize migrate: %w", err)
|
||||
}
|
||||
|
||||
return migrate, nil
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
package cassandradb
|
||||
|
||||
import (
|
||||
"github.com/gocql/gocql"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/config"
|
||||
)
|
||||
|
||||
// ProvideCassandraConnection provides a Cassandra session for Wire DI
|
||||
func ProvideCassandraConnection(cfg *config.Config, logger *zap.Logger) (*gocql.Session, error) {
|
||||
return NewCassandraConnection(cfg, logger)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue