-
Notifications
You must be signed in to change notification settings - Fork 0
Switch to failover_buckets from failover_regions #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -10,9 +10,9 @@ defmodule RemotePersistentTerm.Fetcher.S3 do | |||||
| bucket: String.t(), | ||||||
| key: String.t(), | ||||||
| region: String.t(), | ||||||
| failover_regions: [String.t()] | nil | ||||||
| failover_buckets: [[bucket: String.t(), region: String.t()]] | nil | ||||||
| } | ||||||
| defstruct [:bucket, :key, :region, :failover_regions] | ||||||
| defstruct [:bucket, :key, :region, :failover_buckets] | ||||||
|
|
||||||
| @opts_schema [ | ||||||
| bucket: [ | ||||||
|
|
@@ -30,11 +30,12 @@ defmodule RemotePersistentTerm.Fetcher.S3 do | |||||
| required: true, | ||||||
| doc: "The AWS region of the s3 bucket." | ||||||
| ], | ||||||
| failover_regions: [ | ||||||
| type: {:list, :string}, | ||||||
| failover_buckets: [ | ||||||
| type: {:list, :keyword_list}, | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you define a a schema for the failover buckets, so that nimble options validates it has the correct key/values? |
||||||
| required: false, | ||||||
| doc: | ||||||
| "A list of AWS regions to use if calls to the default region fail. They will be tried in order." | ||||||
| "A list of keyword lists containing [bucket: bucket_name, region: region] to use as failover if the primary bucket fails. \n | ||||||
|
sneako marked this conversation as resolved.
Outdated
|
||||||
| The directory structure in failover buckets must match the primary bucket." | ||||||
| ] | ||||||
| ] | ||||||
|
|
||||||
|
|
@@ -58,7 +59,7 @@ defmodule RemotePersistentTerm.Fetcher.S3 do | |||||
| bucket: valid_opts[:bucket], | ||||||
| key: valid_opts[:key], | ||||||
| region: valid_opts[:region], | ||||||
| failover_regions: valid_opts[:failover_regions] | ||||||
| failover_buckets: valid_opts[:failover_buckets] | ||||||
| }} | ||||||
| end | ||||||
| end | ||||||
|
|
@@ -118,19 +119,19 @@ defmodule RemotePersistentTerm.Fetcher.S3 do | |||||
|
|
||||||
| defp list_object_versions(state) do | ||||||
| res = | ||||||
| state.bucket | ||||||
| |> ExAws.S3.get_bucket_object_versions(prefix: state.key) | ||||||
| |> aws_client_request(state) | ||||||
| aws_client_request( | ||||||
| &ExAws.S3.get_bucket_object_versions/2, | ||||||
| state, | ||||||
| prefix: state.key | ||||||
| ) | ||||||
|
|
||||||
| with {:ok, %{body: %{versions: versions}}} <- res do | ||||||
| {:ok, versions} | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| defp get_object(state) do | ||||||
| state.bucket | ||||||
| |> ExAws.S3.get_object(state.key) | ||||||
| |> aws_client_request(state) | ||||||
| aws_client_request(&ExAws.S3.get_object/2, state, state.key) | ||||||
| end | ||||||
|
|
||||||
| defp find_latest([_ | _] = contents) do | ||||||
|
|
@@ -149,58 +150,67 @@ defmodule RemotePersistentTerm.Fetcher.S3 do | |||||
|
|
||||||
| defp find_latest(_), do: {:error, :not_found} | ||||||
|
|
||||||
| defp aws_client_request(op, %{region: region, failover_regions: nil}), | ||||||
| do: client().request(op, region: region) | ||||||
| defp aws_client_request(op, %{failover_buckets: nil} = state, opts) do | ||||||
| perform_request(op, state.bucket, state.region, opts) | ||||||
| end | ||||||
|
|
||||||
| defp aws_client_request( | ||||||
| op, | ||||||
| %{ | ||||||
| region: region, | ||||||
| bucket: bucket, | ||||||
| key: key, | ||||||
| failover_regions: failover_regions | ||||||
| } = state | ||||||
| failover_buckets: failover_buckets | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
just to make sure the list isn't empty. We can get rid of the guard with this change too |
||||||
| } = state, | ||||||
| opts | ||||||
| ) | ||||||
| when is_list(failover_regions) do | ||||||
| with {:error, reason} <- client().request(op, region: region) do | ||||||
| when is_list(failover_buckets) do | ||||||
| with {:error, reason} <- perform_request(op, state.bucket, state.region, opts) do | ||||||
| Logger.error(%{ | ||||||
| bucket: bucket, | ||||||
| key: key, | ||||||
| region: region, | ||||||
| bucket: state.bucket, | ||||||
| key: state.key, | ||||||
| region: state.region, | ||||||
| reason: inspect(reason), | ||||||
| message: "Failed to fetch from primary region, attempting failover regions" | ||||||
| message: "Failed to fetch from primary bucket, attempting failover buckets" | ||||||
| }) | ||||||
|
|
||||||
| try_failover_regions(op, failover_regions, state) | ||||||
| try_failover_buckets(op, failover_buckets, opts, state) | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| defp try_failover_regions(_op, [], _state), do: {:error, "All regions failed"} | ||||||
| defp try_failover_buckets(_op, [], _opts, _state), do: {:error, "All buckets failed"} | ||||||
|
|
||||||
| defp try_failover_regions(op, [region | remaining_regions], state) do | ||||||
| defp try_failover_buckets( | ||||||
| op, | ||||||
| [[bucket: bucket, region: region] | remaining_buckets], | ||||||
| opts, | ||||||
| state | ||||||
| ) do | ||||||
| Logger.info(%{ | ||||||
| bucket: state.bucket, | ||||||
| bucket: bucket, | ||||||
| key: state.key, | ||||||
| region: region, | ||||||
| message: "Trying failover region" | ||||||
| message: "Trying failover bucket" | ||||||
| }) | ||||||
|
|
||||||
| case client().request(op, region: region) do | ||||||
| case perform_request(op, bucket, region, opts) do | ||||||
| {:ok, result} -> | ||||||
| {:ok, result} | ||||||
|
|
||||||
| {:error, reason} -> | ||||||
| Logger.error(%{ | ||||||
| bucket: state.bucket, | ||||||
| bucket: bucket, | ||||||
| key: state.key, | ||||||
| region: region, | ||||||
| reason: inspect(reason), | ||||||
| message: "Failed to fetch from failover region" | ||||||
| message: "Failed to fetch from failover bucket" | ||||||
| }) | ||||||
|
|
||||||
| try_failover_regions(op, remaining_regions, state) | ||||||
| try_failover_buckets(op, remaining_buckets, opts, state) | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| defp perform_request(op, bucket, region, opts) do | ||||||
| op.(bucket, opts) | ||||||
| |> client().request(region: region) | ||||||
| end | ||||||
|
|
||||||
| defp client, do: Application.get_env(:remote_persistent_term, :aws_client, ExAws) | ||||||
| end | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's define a
failover_bucketas a new typeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe even types for bucket and region since we use them more than once