Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand C#.NET distributed Lock Service

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article to share with you is about how to understand C#. NET distributed lock service, Xiaobian think quite practical, so share to everyone to learn, I hope you can read this article after some harvest, not much to say, follow Xiaobian to see it.

background

Distributed locking services may not be used much in your projects because everyone puts exclusivity at the database level. When a large number of row locks, table locks, and transactions flood the database, it is better to think differently. A lot of bottlenecks in general web applications are on the database, here is a solution to reduce the burden of database locks.

briefing

If our requirements are simple, such as for the user's account funds, to ensure atomic operation. And different clients can submit only one object operation at a time. Lock, single case?! On a single platform, it is OK, but on large-scale web projects, Load Balancer is a common technical means. There may be different copies of objects with the same meaning. At this time, how can we ensure exclusive operation? Database transactions! In addition to this, we bring you to the topic of this chapter, distributed locking services.

A simple lock service is not difficult to implement, and even memcache can be used to quickly construct a distributed lock system. We only need to write kv key-value pairs when operating on objects, release kv when the operation is complete, and determine whether kv has data when reading objects. We can even give it a default release time.

This is a solution, but if we were a bit more demanding, we would need authorization (e.g. requests from xxx domain names only), hierarchical node associations (e.g. a user's funds account is locked, his shopping cart, credits, etc. are locked), monitor callbacks, and even single point failures. Well, bugs recommend another solution here-zookeeper.

Zookeeper is a module in Hadoop. Is a distributed, open-source distributed application coordination service that can be used to synchronize services, configuration maintenance.

For more content, please read the document or search online directly. The theoretical content is written too much to make people sleepy. Let's look directly at practice.

Performance

Server ubuntu (one virtual machine)

Client window2003

Install java environment on server and deploy it according to official introduction

Start zkserver

We test lock service-related operations

ps: try windows2003 of this machine because it is a local environment, not to compare with the above, just to see the data processing efficiency of zookeeper itself.

Function

One picture can explain the general function

Take another look at Watcher.

public class MyWatch : IWatcher { public void Process(WatchedEvent qevent) { Console.WriteLine("this is MyWatch"); } } public class MyWatch3 : IWatcher { public void Process(WatchedEvent qevent) { Console.WriteLine("this is MyWatch3"); } }

new ZooKeeper("192.168.206.129:2181", new TimeSpan(0, 0, 0, 4000), new MyWatch()) when creating a connection;

zk.Exists(Dir, new MyWatch3 ());

GetData(Dir, new MyWatch3 (), stat);

Let's run the demo again and remove the delete operation

Add delete operation

shallow analysis

Create a connection:

Get a list of service hosts

Set timeout

Register Client Events

Create request connections in a thread-safe manner (start client request queue, circular queue based on socket communication, perform different request actions depending on request type)

Request Flow:

Construct request header, construct request, reply, construct response header, construct Packet object, when packet object is ready, put the whole object into an outgoingQueue

The packet is placed in the outgoingQueue, waiting for SendThread to send the contents of the packet to the server. Server processing is performed in 3 steps in the doio method ReadLength ReadConnectResult ReadResponse until the packet request is determined to be complete in the ReadResponse method.

Response process:

resp for ping requests for heartbeats, resp for auth requests, resp for general interface requests, notification when watcher concerns change if interface requests require watcher

API methods for lock-related parts:

Create node: create

demo:zk.Create(Dir, severname.GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);

Create Mode is divided into four categories: Persistent, PersistentSequential, Ephemeral, EphemeralSequential.

PERSISTENT Create persistent nodes, nodes/data will not disappear after the corresponding machine is closed

PERSISTENT_SEQUENTIAL If PATH ends with '/' then create a child node with the PATH as parent node, and the child node name is a numerical value in sequence; otherwise create a node with the name of the character after '/' plus a numerical string in sequence, and also create a persistent node.

EPHEMERAL creates transient nodes, Zookeeper clears transient nodes it creates when it senses a connected machine outage

EPHEMERAL_SEQUENTIAL is a transient sequential node, just like PERSISTENT_SEQUENTIAL, except that it is transient.

delete node delete

demo :zk.Delete(Dir, -1);

The first parameter represents the node name (usually used as a path), and the second parameter is the version number-1 for a full match

View node exists

demo : zk.Exists(Dir, new MyWatch3());

Get Data getData

demo :zk.GetData(Dir, new MyWatch3(), stat);

Get the data of a node, which can be injected into the watcher

setData

demo : zk.SetData(Dir, new byte[1], 1);

Get child node collection GetChildren

demo :zk.GetChildren(Dir, true);

storing

Znodes are similar to files and directories. But it's not a typical file system, zookeeper data is kept in memory, which means zookeeper can achieve high throughput and low latency.

watcher

Zookeeper has two types of watches: data watches and child watches. getData() and exists() and create() add data watches, getChildren() adds child watches. Delete() involves deleting data and child nodes, triggering both data watches and child watches.

For details, please refer to www.geminikwok.com/2011/09/08/%E6%B5%85%E8%B0%88zookeeper-watch%E4%BA%8B%E4%BB%B6/

arithmetic

Paoxs algorithm in this article only a single server to do demo.

The above is how to understand C#. NET distributed lock service, Xiaobian believes that some knowledge points may be what we see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report