Skip to content

Commit 38e720d

Browse files
authored
Bigtable row affinity (#12090)
1 parent 665013b commit 38e720d

File tree

5 files changed

+152
-1
lines changed

5 files changed

+152
-1
lines changed

mmv1/templates/terraform/custom_expand/bigtable_app_profile_routing.tmpl

+7
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,12 @@ func expand{{$.GetPrefix}}{{$.TitlelizeProperty}}(v interface{}, d tpgresource.T
2323
obj.ClusterIds = append(obj.ClusterIds, id.(string))
2424
}
2525

26+
affinity, _ := d.GetOkExists("row_affinity")
27+
if affinity != nil && affinity == true {
28+
obj.RowAffinity = &bigtableadmin.RowAffinity{}
29+
} else {
30+
obj.RowAffinity = nil
31+
}
32+
2633
return obj, nil
2734
}

mmv1/templates/terraform/custom_flatten/bigtable_app_profile_routing.tmpl

+5
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@
1111
limitations under the License.
1212
*/ -}}
1313
func flatten{{$.GetPrefix}}{{$.TitlelizeProperty}}(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
14+
d.Set("row_affinity", nil)
1415
if v == nil {
1516
return false
1617
}
1718

19+
if v.(map[string]interface{})["rowAffinity"] != nil {
20+
d.Set("row_affinity", true)
21+
}
22+
1823
if v.(map[string]interface{})["clusterIds"] == nil {
1924
return true
2025
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
resource "google_bigtable_instance" "instance" {
2+
name = "{{index $.Vars "instance_name"}}"
3+
cluster {
4+
cluster_id = "cluster-1"
5+
zone = "us-central1-a"
6+
num_nodes = 3
7+
storage_type = "HDD"
8+
}
9+
cluster {
10+
cluster_id = "cluster-2"
11+
zone = "us-central1-b"
12+
num_nodes = 3
13+
storage_type = "HDD"
14+
}
15+
cluster {
16+
cluster_id = "cluster-3"
17+
zone = "us-central1-c"
18+
num_nodes = 3
19+
storage_type = "HDD"
20+
}
21+
22+
deletion_protection = "{{index $.Vars "deletion_protection"}}"
23+
}
24+
25+
resource "google_bigtable_app_profile" "ap" {
26+
instance = google_bigtable_instance.instance.name
27+
app_profile_id = "{{index $.Vars "app_profile_name"}}"
28+
29+
// Requests will be routed to the following 2 clusters.
30+
multi_cluster_routing_use_any = true
31+
multi_cluster_routing_cluster_ids = ["cluster-1", "cluster-2"]
32+
33+
row_affinity = true
34+
35+
ignore_warnings = true
36+
}
37+

mmv1/templates/terraform/extra_schema_entry/bigtable_app_profile.go.tmpl

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,10 @@
1818
Type: schema.TypeString,
1919
},
2020
ConflictsWith: []string{"single_cluster_routing"},
21-
},
21+
},
22+
"row_affinity": {
23+
Type: schema.TypeBool,
24+
Optional: true,
25+
Description: `Must be used with multi-cluster routing. If true, then this app profile will use row affinity sticky routing. With row affinity, Bigtable will route single row key requests based on the row key, rather than randomly. Instead, each row key will be assigned to a cluster by Cloud Bigtable, and will stick to that cluster. Choosing this option improves read-your-writes consistency for most requests under most circumstances, without sacrificing availability. Consistency is not guaranteed, as requests may still fail over between clusters in the event of errors or latency.`,
26+
ConflictsWith: []string{"single_cluster_routing"},
27+
},

mmv1/third_party/terraform/services/bigtable/resource_bigtable_app_profile_test.go

+96
Original file line numberDiff line numberDiff line change
@@ -566,3 +566,99 @@ func TestAccBigtableAppProfile_updateStandardIsolationToDataBoost(t *testing.T)
566566
},
567567
})
568568
}
569+
570+
func testAccBigtableAppProfile_updateRowAffinity(instanceName string) string {
571+
return fmt.Sprintf(`
572+
resource "google_bigtable_instance" "instance" {
573+
name = "%s"
574+
cluster {
575+
cluster_id = "%s"
576+
zone = "us-central1-b"
577+
num_nodes = 1
578+
storage_type = "HDD"
579+
}
580+
cluster {
581+
cluster_id = "%s2"
582+
zone = "us-central1-a"
583+
num_nodes = 1
584+
storage_type = "HDD"
585+
}
586+
cluster {
587+
cluster_id = "%s3"
588+
zone = "us-central1-c"
589+
num_nodes = 1
590+
storage_type = "HDD"
591+
}
592+
deletion_protection = false
593+
}
594+
resource "google_bigtable_app_profile" "ap" {
595+
instance = google_bigtable_instance.instance.id
596+
app_profile_id = "test"
597+
multi_cluster_routing_use_any = true
598+
multi_cluster_routing_cluster_ids = ["%s", "%s2", "%s3"]
599+
row_affinity = true
600+
ignore_warnings = true
601+
}
602+
`, instanceName, instanceName, instanceName, instanceName, instanceName, instanceName, instanceName)
603+
}
604+
605+
func TestAccBigtableAppProfile_updateRowAffinity(t *testing.T) {
606+
// bigtable instance does not use the shared HTTP client, this test creates an instance
607+
acctest.SkipIfVcr(t)
608+
t.Parallel()
609+
610+
instanceName := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
611+
612+
acctest.VcrTest(t, resource.TestCase{
613+
PreCheck: func() { acctest.AccTestPreCheck(t) },
614+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
615+
CheckDestroy: testAccCheckBigtableAppProfileDestroyProducer(t),
616+
Steps: []resource.TestStep{
617+
{
618+
Config: testAccBigtableAppProfile_updateMC2(instanceName),
619+
},
620+
{
621+
ResourceName: "google_bigtable_app_profile.ap",
622+
ImportState: true,
623+
ImportStateVerify: true,
624+
ImportStateVerifyIgnore: []string{"ignore_warnings"},
625+
},
626+
{
627+
Config: testAccBigtableAppProfile_updateRowAffinity(instanceName),
628+
},
629+
{
630+
ResourceName: "google_bigtable_app_profile.ap",
631+
ImportState: true,
632+
ImportStateVerify: true,
633+
ImportStateVerifyIgnore: []string{"ignore_warnings"},
634+
},
635+
{
636+
Config: testAccBigtableAppProfile_update1(instanceName),
637+
},
638+
{
639+
ResourceName: "google_bigtable_app_profile.ap",
640+
ImportState: true,
641+
ImportStateVerify: true,
642+
ImportStateVerifyIgnore: []string{"ignore_warnings"},
643+
},
644+
{
645+
Config: testAccBigtableAppProfile_updateRowAffinity(instanceName),
646+
},
647+
{
648+
ResourceName: "google_bigtable_app_profile.ap",
649+
ImportState: true,
650+
ImportStateVerify: true,
651+
ImportStateVerifyIgnore: []string{"ignore_warnings"},
652+
},
653+
{
654+
Config: testAccBigtableAppProfile_updateMC2(instanceName),
655+
},
656+
{
657+
ResourceName: "google_bigtable_app_profile.ap",
658+
ImportState: true,
659+
ImportStateVerify: true,
660+
ImportStateVerifyIgnore: []string{"ignore_warnings"},
661+
},
662+
},
663+
})
664+
}

0 commit comments

Comments
 (0)