protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

Does MessageLite::SerializeAsString generate unique string for same proto ?

Open quanjielin opened this issue 7 years ago • 4 comments

  1. In blow code, we used SerializeAsString with the hope that it generates unique string for same proto (so that it could be used as mapkey)
const std::string map_key = sds_config_source.SerializeAsString() + config_name;
  1. However from logs from commit; the result from SerializeAsString for same proto are different
[2019-02-01 23:03:12.792][14][debug][upstream] [external/envoy/source/common/upstream/cds_api_impl.cc:57] cds: add/update cluster 'outbound|443||kubernetes.default.svc.cluster.local'
[2019-02-01 23:03:12.796][14][info][secret] [bazel-out/k8-fastbuild/bin/external/envoy/source/common/secret/_virtual_includes/secret_manager_impl_lib/common/secret/secret_manager_impl.h:57] ***config name default
[2019-02-01 23:03:12.796][14][info][secret] [bazel-out/k8-fastbuild/bin/external/envoy/source/common/secret/_virtual_includes/secret_manager_impl_lib/common/secret/secret_manager_impl.h:58] ***sds_config_source api_config_source {
  api_type: GRPC
  grpc_services {
    google_grpc {
      target_uri: "unix:/var/run/sds/uds_path"
      channel_credentials {
        local_credentials {
        }
      }
      call_credentials {
        from_plugin {
          name: "envoy.grpc_credentials.file_based_metadata"
          config {
            fields {
              key: "header_key"
              value {
                string_value: "istio_sds_credentail_header-bin"
              }
            }
            fields {
              key: "secret_data"
              value {
                struct_value {
                  fields {
                    key: "filename"
                    value {
                      string_value: "/var/run/secrets/kubernetes.io/serviceaccount/token"
                    }
                  }
                }
              }
            }
          }
        }
      }
      stat_prefix: "sdsstat"
      credentials_factory_name: "envoy.grpc_credentials.file_based_metadata"
    }
  }
}

[2019-02-01 23:03:12.796][14][info][secret] [bazel-out/k8-fastbuild/bin/external/envoy/source/common/secret/_virtual_includes/secret_manager_impl_lib/common/secret/secret_manager_impl.h:60] ***sds_config_source binary 
EpkCCAIilAISkQIKGnVuaXg6L3Zhci9ydW4vc2RzL3Vkc19wYXRoEgIaABq5ATK2AQoqZW52b3kuZ3JwY19jcmVkZW50aWFscy5maWxlX2Jhc2VkX21ldGFkYXRhEocBCi8KCmhlYWRlcl9rZXkSIRofaXN0aW9fc2RzX2NyZWRlbnRhaWxfaGVhZGVyLWJpbgpUCgtzZWNyZXRfZGF0YRJFKkMKQQoIZmlsZW5hbWUSNRozL3Zhci9ydW4vc2VjcmV0cy9rdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3Rva2VuIgdzZHNzdGF0KiplbnZveS5ncnBjX2NyZWRlbnRpYWxzLmZpbGVfYmFzZWRfbWV0YWRhdGE=

[2019-02-01 23:03:12.796][14][info][secret] [bazel-out/k8-fastbuild/bin/external/envoy/source/common/secret/_virtual_includes/secret_manager_impl_lib/common/secret/secret_manager_impl.h:61] ***mapkey hash_key 462951205992374496

[2019-02-01 23:03:12.796][14][info][secret] [bazel-out/k8-fastbuild/bin/external/envoy/source/common/secret/_virtual_includes/secret_manager_impl_lib/common/secret/secret_manager_impl.h:73] ***add secret provider for default

...
[2019-02-01 23:03:12.829][14][debug][upstream] [external/envoy/source/common/upstream/cds_api_impl.cc:57] cds: add/update cluster 'outbound|9901||istio-galley.istio-system.svc.cluster.local'
[2019-02-01 23:03:12.831][14][info][secret] [bazel-out/k8-fastbuild/bin/external/envoy/source/common/secret/_virtual_includes/secret_manager_impl_lib/common/secret/secret_manager_impl.h:57] ***config name default
[2019-02-01 23:03:12.831][14][info][secret] [bazel-out/k8-fastbuild/bin/external/envoy/source/common/secret/_virtual_includes/secret_manager_impl_lib/common/secret/secret_manager_impl.h:58] ***sds_config_source api_config_source {
  api_type: GRPC
  grpc_services {
    google_grpc {
      target_uri: "unix:/var/run/sds/uds_path"
      channel_credentials {
        local_credentials {
        }
      }
      call_credentials {
        from_plugin {
          name: "envoy.grpc_credentials.file_based_metadata"
          config {
            fields {
              key: "header_key"
              value {
                string_value: "istio_sds_credentail_header-bin"
              }
            }
            fields {
              key: "secret_data"
              value {
                struct_value {
                  fields {
                    key: "filename"
                    value {
                      string_value: "/var/run/secrets/kubernetes.io/serviceaccount/token"
                    }
                  }
                }
              }
            }
          }
        }
      }
      stat_prefix: "sdsstat"
      credentials_factory_name: "envoy.grpc_credentials.file_based_metadata"
    }
  }
}

[2019-02-01 23:03:12.832][14][info][secret] [bazel-out/k8-fastbuild/bin/external/envoy/source/common/secret/_virtual_includes/secret_manager_impl_lib/common/secret/secret_manager_impl.h:60] ***sds_config_source binary 
EpkCCAIilAISkQIKGnVuaXg6L3Zhci9ydW4vc2RzL3Vkc19wYXRoEgIaABq5ATK2AQoqZW52b3kuZ3JwY19jcmVkZW50aWFscy5maWxlX2Jhc2VkX21ldGFkYXRhEocBClQKC3NlY3JldF9kYXRhEkUqQwpBCghmaWxlbmFtZRI1GjMvdmFyL3J1bi9zZWNyZXRzL2t1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvdG9rZW4KLwoKaGVhZGVyX2tleRIhGh9pc3Rpb19zZHNfY3JlZGVudGFpbF9oZWFkZXItYmluIgdzZHNzdGF0KiplbnZveS5ncnBjX2NyZWRlbnRpYWxzLmZpbGVfYmFzZWRfbWV0YWRhdGE=

[2019-02-01 23:03:12.832][14][info][secret] [bazel-out/k8-fastbuild/bin/external/envoy/source/common/secret/_virtual_includes/secret_manager_impl_lib/common/secret/secret_manager_impl.h:61] ***mapkey hash_key 12493521696261848690

[2019-02-01 23:03:12.832][14][info][secret] [bazel-out/k8-fastbuild/bin/external/envoy/source/common/secret/_virtual_includes/secret_manager_impl_lib/common/secret/secret_manager_impl.h:73] ***add secret provider for default
  1. If SerializeAsString doesn't grantee uniqueness, is there any other function to generate unique key for same proto ?

quanjielin avatar Feb 01 '19 23:02 quanjielin

/cc @diemtvu @qiwzhang

quanjielin avatar Feb 01 '19 23:02 quanjielin

Serialization is not guaranteed to be deterministic, and the usual reason for that is that map fields are serialized in an unspecified order. However, if you need the serialization to be deterministic then you can accomplish that by serializing to a CodedOutputStream after calling SetSerializationDeterministic(true) on that output stream. But keep in mind that if you do this the serialization is only guaranteed to be deterministic for a given build. It might change over time as you modify your code or if you upgrade your protobuf version. As a result this will only make sense for something like a temporary in-memory map, but will not work well for keys stored in a persistent database.

acozzette avatar Feb 01 '19 23:02 acozzette

Thanks @acozzette ! Temporary in-memory map should be good enough for our case, seems envoy already has such a hash function, just switched to it and the duplication issue goes away.

quanjielin avatar Feb 02 '19 01:02 quanjielin

Great, I'm glad you were able to find a solution.

acozzette avatar Feb 05 '19 00:02 acozzette