zaptidgen

command module
v0.0.0-...-6caf283 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 15, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

README

zaptidgen

A grpc uint64 sequence generator based on Sony Snowflake

This code is a Go implementation of a gRPC service that generates unique IDs using the Snowflake algorithm, specifically the Sonyflake variant. Why use it? INT keys have better performance than UUID keys, and they can improve partition-based database sharding.

The Snowflake algorithm is essential in distributed systems for generating unique identifiers across multiple nodes without centralized coordination. This capability is crucial for a variety of applications, particularly in environments where scaling horizontally (adding more machines or processes) is common. Here are some key reasons for the need and adoption of the Snowflake algorithm:

  1. Scalability: As systems grow and handle more data, they often need to distribute that data across many servers. The Snowflake algorithm enables each node in a distributed system to generate IDs independently without requiring a round-trip to a centralized database or service. This significantly reduces bottlenecks and latency associated with ID generation, facilitating scalability.

  2. Uniqueness: In distributed systems, ensuring that records or entities have unique identifiers is crucial to avoid conflicts (like data overwrite issues) and maintain data integrity. Snowflake generates IDs that are unique across all nodes without needing synchronization between them.

  3. Rough Time-Ordering: Snowflake IDs incorporate the system’s timestamp, which means they are sortable and roughly indicate the creation time of the respective record. This time-based component is beneficial for debugging, data sorting, and time-series analysis, as it allows entities to be ordered without additional timestamp fields.

  4. High Performance: Snowflake can generate a large number of IDs in a very short time, which is critical in high-throughput systems where many entities are created every second. The performance impact of ID generation is minimal, allowing systems to maintain high transaction rates.

  5. Simple Implementation and Low Overhead: The algorithm is relatively simple to implement and does not require complex infrastructure or significant computing resources. This makes it an attractive choice for many systems, especially those with limited resources or requiring a lightweight solution.

  6. Decentralized Control: By avoiding the need for a central authority to issue IDs, Snowflake eliminates a single point of failure and a potential performance bottleneck in distributed systems. This decentralized approach enhances the system’s resilience and reliability.

Overall, the Snowflake algorithm meets the critical needs of modern distributed systems by providing a fast, efficient, and reliable method for generating unique identifiers. This capability supports broader architectural goals such as fault tolerance, scalability, and performance efficiency.

Here’s an explanation of the key components and their roles:

  1. Package Imports:

    • The code imports several packages needed for network communication, error handling, and logging. Notably, it uses the sonyflake package for generating unique IDs and grpc for setting up the gRPC server.
  2. Global Variables:

    • VERSION specifies the version of the server.
    • logger is used for logging server events and errors.
    • flake is an instance of the Sonyflake generator, which is used to produce unique IDs.
  3. Sonyflake Algorithm:

    • Sonyflake is a distributed unique ID generator inspired by Twitter's Snowflake. It's designed to generate IDs that are roughly sortable by time and unique across multiple machines without coordination between them.
    • The generator is initialized with default settings, which usually include the current machine's timestamp, sequence number, and machine ID.
  4. gRPC Server Setup:

    • The server struct implements the IdGenServer interface generated from the protobuf definition. It includes a method Gen to handle ID generation requests.
    • The Gen method checks if the Sonyflake instance is properly initialized, generates the next ID, handles any errors during generation, and logs appropriate messages.
  5. gRPC Service Method (Gen):

    • This method receives a request for an ID, generates it using the flake.NextID() call, and returns the generated ID in a response. It also handles errors if the ID generation fails.
  6. Main Function:

    • Logs the start of the server and its version.
    • Sets up the network listener on TCP port 8888 for incoming gRPC connections.
    • Registers the server implementation with the gRPC framework and starts listening for requests. It also handles any errors that occur during server setup or execution, such as failure to bind to the port or to serve incoming requests.
  7. Execution and Error Handling:

    • The server will panic and log fatal errors if it cannot start properly, such as if it fails to bind to the required port or the ID generator fails to initialize.

This implementation ensures that the server is robust, logging detailed error messages and handling possible initialization errors gracefully. It provides a scalable solution for systems requiring unique identifiers for objects or entities across distributed environments.

Client code

The provided client code (folder "client") is a Go client for the gRPC service we discussed earlier, which generates unique IDs using the Snowflake algorithm through a Sonyflake implementation. Here's a breakdown of how the client functions:

  1. Package Imports:

    • The client imports necessary packages for setting up gRPC connections, handling contexts for timeouts, and formatting output.
  2. Connection Setup:

    • The client establishes a connection to the gRPC server at localhost:8888 using grpc.Dial. It uses insecure.NewCredentials() for the connection, which implies that the connection does not use TLS. This is common in development environments but should be replaced with secure credentials in production.
  3. Client Initialization:

    • After successfully establishing the connection, it defers the closure of this connection (conn.Close()) to ensure that resources are properly cleaned up when the program finishes or if an error occurs.
    • It then creates a new client instance (c) for the ID generation service using the connection.
  4. Context Handling:

    • The client prepares a context with a timeout (context.WithTimeout), specifying that the request must complete within one second. This is important to avoid hanging requests and to manage latency and resource allocation effectively. The defer cancel() ensures that resources related to the context are released once the request is completed or timed out.
  5. Making a Request:

    • The client constructs an IdRequest object, which is empty in this case as the service does not require any specific input data to generate an ID.
    • It calls the Gen method on the service client, passing the context and the request object. This method call is where the client interacts with the server to receive a new unique ID.
  6. Handling Responses and Errors:

    • The response from the Gen method contains the generated ID or an error if something goes wrong (e.g., network issues, server errors). The client handles this by checking the error and then printing the ID received from the server.
    • The output includes logging the received ID to the console using fmt.Println. If there's an error in connecting to the server or in the ID generation, the client will output the error and terminate (panic("did not connect")).

This client code demonstrates a simple but effective way of interacting with a gRPC service for obtaining unique IDs. It's structured to handle basic error scenarios and ensure that system resources are managed efficiently with proper context handling and connection management.

Installation

Download the code, start the server:

go run zaptidgen.go

Try the client:

go run client/main.go

Documentation

Overview

Copyright 2023 Cleuton Sampaio.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL