Skip to content

Commit 2293d0b

Browse files
Bigtable row affinity (#12090) (#20435)
[upstream:38e720d9bdd6cf78763ec550815ed74b5f07047e] Signed-off-by: Modular Magician <[email protected]>
1 parent 9f761d3 commit 2293d0b

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

.changelog/12090.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
bigtable: added `row_affinity` field to `google_bigtable_app_profile` resource
3+
```

google/services/bigtable/resource_bigtable_app_profile.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,12 @@ It is unsafe to send these requests to the same table/row/column in multiple clu
159159
},
160160
ConflictsWith: []string{"single_cluster_routing"},
161161
},
162+
"row_affinity": {
163+
Type: schema.TypeBool,
164+
Optional: true,
165+
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.`,
166+
ConflictsWith: []string{"single_cluster_routing"},
167+
},
162168
"project": {
163169
Type: schema.TypeString,
164170
Optional: true,
@@ -556,10 +562,15 @@ func flattenBigtableAppProfileDescription(v interface{}, d *schema.ResourceData,
556562
}
557563

558564
func flattenBigtableAppProfileMultiClusterRoutingUseAny(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
565+
d.Set("row_affinity", nil)
559566
if v == nil {
560567
return false
561568
}
562569

570+
if v.(map[string]interface{})["rowAffinity"] != nil {
571+
d.Set("row_affinity", true)
572+
}
573+
563574
if v.(map[string]interface{})["clusterIds"] == nil {
564575
return true
565576
}
@@ -647,6 +658,13 @@ func expandBigtableAppProfileMultiClusterRoutingUseAny(v interface{}, d tpgresou
647658
obj.ClusterIds = append(obj.ClusterIds, id.(string))
648659
}
649660

661+
affinity, _ := d.GetOkExists("row_affinity")
662+
if affinity != nil && affinity == true {
663+
obj.RowAffinity = &bigtableadmin.RowAffinity{}
664+
} else {
665+
obj.RowAffinity = nil
666+
}
667+
650668
return obj, nil
651669
}
652670

google/services/bigtable/resource_bigtable_app_profile_test.go

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

0 commit comments

Comments
 (0)