Class: LaunchDarkly::Impl::Migrations::Migrator

Inherits:
Object
  • Object
show all
Includes:
LaunchDarkly::Interfaces::Migrations::Migrator
Defined in:
lib/ldclient-rb/impl/migrations/migrator.rb

Overview

An implementation of the [LaunchDarkly::Interfaces::Migrations::Migrator] interface, capable of supporting feature-flag backed technology migrations.

Since:

  • 5.5.0

Instance Method Summary collapse

Constructor Details

#initialize(client, read_execution_order, read_config, write_config, measure_latency, measure_errors) ⇒ Migrator

Returns a new instance of Migrator.

Parameters:

Since:

  • 5.5.0



67
68
69
70
71
72
73
74
75
# File 'lib/ldclient-rb/impl/migrations/migrator.rb', line 67

def initialize(client, read_execution_order, read_config, write_config, measure_latency, measure_errors)
  @client = client
  @read_execution_order = read_execution_order
  @read_config = read_config
  @write_config = write_config
  @measure_latency = measure_latency
  @measure_errors = measure_errors
  @sampler = LaunchDarkly::Impl::Sampler.new(Random.new)
end

Instance Method Details

#read(key, context, default_stage, payload = nil) ⇒ LaunchDarkly::Migrations::OperationResult

Perform the configured read operations against the appropriate old and/or new origins.

Parameters:

  • key (String)

    The migration-based flag key to use for determining migration stages

  • context (LaunchDarkly::LDContext)

    The context to use for evaluating the migration flag

  • default_stage (Symbol)

    The stage to fallback to if one could not be determined for the requested flag

  • payload (String) (defaults to: nil)

    An optional payload to pass through to the configured read operations.

Returns:

Since:

  • 5.5.0



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ldclient-rb/impl/migrations/migrator.rb', line 87

def read(key, context, default_stage, payload = nil)
  stage, tracker = @client.migration_variation(key, context, default_stage)
  tracker.operation(LaunchDarkly::Migrations::OP_READ)

  old = Executor.new(@client.logger, LaunchDarkly::Migrations::ORIGIN_OLD, @read_config.old, tracker, @measure_latency, @measure_errors, payload)
  new = Executor.new(@client.logger, LaunchDarkly::Migrations::ORIGIN_NEW, @read_config.new, tracker, @measure_latency, @measure_errors, payload)

  case stage
  when LaunchDarkly::Migrations::STAGE_OFF
    result = old.run
  when LaunchDarkly::Migrations::STAGE_DUALWRITE
    result = old.run
  when LaunchDarkly::Migrations::STAGE_SHADOW
    result = read_both(old, new, @read_config.comparison, @read_execution_order, tracker)
  when LaunchDarkly::Migrations::STAGE_LIVE
    result = read_both(new, old, @read_config.comparison, @read_execution_order, tracker)
  when LaunchDarkly::Migrations::STAGE_RAMPDOWN
    result = new.run
  when LaunchDarkly::Migrations::STAGE_COMPLETE
    result = new.run
  else
    result = LaunchDarkly::Migrations::OperationResult.new(
      LaunchDarkly::Migrations::ORIGIN_OLD,
      LaunchDarkly::Result.fail("invalid stage #{stage}; cannot execute read")
    )
  end

  @client.track_migration_op(tracker)

  result
end

#write(key, context, default_stage, payload = nil) ⇒ LaunchDarkly::Migrations::WriteResult

Perform the configured write operations against the appropriate old and/or new origins.

Parameters:

  • key (String)

    The migration-based flag key to use for determining migration stages

  • context (LaunchDarkly::LDContext)

    The context to use for evaluating the migration flag

  • default_stage (Symbol)

    The stage to fallback to if one could not be determined for the requested flag

  • payload (String) (defaults to: nil)

    An optional payload to pass through to the configured write operations.

Returns:

Since:

  • 5.5.0



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/ldclient-rb/impl/migrations/migrator.rb', line 129

def write(key, context, default_stage, payload = nil)
  stage, tracker = @client.migration_variation(key, context, default_stage)
  tracker.operation(LaunchDarkly::Migrations::OP_WRITE)

  old = Executor.new(@client.logger, LaunchDarkly::Migrations::ORIGIN_OLD, @write_config.old, tracker, @measure_latency, @measure_errors, payload)
  new = Executor.new(@client.logger, LaunchDarkly::Migrations::ORIGIN_NEW, @write_config.new, tracker, @measure_latency, @measure_errors, payload)

  case stage
  when LaunchDarkly::Migrations::STAGE_OFF
    result = old.run()
    write_result = LaunchDarkly::Migrations::WriteResult.new(result)
  when LaunchDarkly::Migrations::STAGE_DUALWRITE
    authoritative_result, nonauthoritative_result = write_both(old, new, tracker)
    write_result = LaunchDarkly::Migrations::WriteResult.new(authoritative_result, nonauthoritative_result)
  when LaunchDarkly::Migrations::STAGE_SHADOW
    authoritative_result, nonauthoritative_result = write_both(old, new, tracker)
    write_result = LaunchDarkly::Migrations::WriteResult.new(authoritative_result, nonauthoritative_result)
  when LaunchDarkly::Migrations::STAGE_LIVE
    authoritative_result, nonauthoritative_result = write_both(new, old, tracker)
    write_result = LaunchDarkly::Migrations::WriteResult.new(authoritative_result, nonauthoritative_result)
  when LaunchDarkly::Migrations::STAGE_RAMPDOWN
    authoritative_result, nonauthoritative_result = write_both(new, old, tracker)
    write_result = LaunchDarkly::Migrations::WriteResult.new(authoritative_result, nonauthoritative_result)
  when LaunchDarkly::Migrations::STAGE_COMPLETE
    result = new.run()
    write_result = LaunchDarkly::Migrations::WriteResult.new(result)
  else
    result = LaunchDarkly::Migrations::OperationResult.fail(
      LaunchDarkly::Migrations::ORIGIN_OLD,
      LaunchDarkly::Result.fail("invalid stage #{stage}; cannot execute write")
    )
    write_result = LaunchDarkly::Migrations::WriteResult.new(result)
  end

  @client.track_migration_op(tracker)

  write_result
end