首页 > 其他分享 >Openstack4j best practice-Multi Thread Token Sharing

Openstack4j best practice-Multi Thread Token Sharing

时间:2023-05-16 16:04:02浏览次数:42  
标签:Multi Sharing Thread thread Token token client Openstack

Openstack4j Multi-thread Token Sharing

Author: slankka

Country/Region: China Mainland

Languages skills: English but not well

Keywords

  • Cloud Virtual Machine
  • Paas
  • Openstack
  • Multi-threading

Background

We are building a Virtual Machine Platform as Paas, these days I'm facing a challenge to access multiple openstack clusters.

The disadvantage of Openstack4j:

  • lacking of automatic token renewer
  • binding token in ThreadLocal

which will cause token sharing problem in multi-thread senario.

Solution found

Here is Openstack official solution:
Using the Same Client Between Threads

the key feature is:

OSFactory.clientFromToken(token);

I got inspired by this example: we can obtain an exists token, and create a new client in my own thread.

Design and implementation

Using Openstack4j for multi-thread or multi-cluster senario

This is a design of mine. It finally solved problems above.

I'm using functional programming and simple threadpool of java core technique.

The picture demostrates these parts from left to right:
Thread sources: UI actions are made by users, or some automatic framework such as cron scheduler.
Service Layer:Actions are converted to Functional objects such as Runnable instances.
Client Proxy: Execute those Runnable objects asynchronously.

  • Create a new client based on that token.
  • Execute and wait for Future(result).

Openstack Contract: Consists of TokenManager and Scheduled Renewer.
TokenManager is responsible for managing multiple clusters token. The key concept is using ThreadPool's core thread to store and retrieve Token. It exposes three public methods and a cluster specific token sharing storage.

  • ensureOpen() authenticate and re-authenticate when expired
  • getClient() create client object
  • checkToken() check expiration

Make sure all method calling of above are invoked under different thread.

Scheduled Renewer is responsible for renew token when expired. It manages an SingleThreadScheduledExecutor for keeping token available, and same amount of clusters of authenticating threads, which are implemented by fixedThreadPool.

  • initialize first connection, scheduleWithFixedDelay.
  • Call ensureOpen periodically then update token sharing storage.

It's fine that a authenticating thread execute and finished, because it is also a core thread and alive, the token are always stored in ThreadLocal of its.

Lifecycle of thread management are omitted

And example:
Example:

Function<OSClient.OSClientV3, ActionResponse> action = (x) -> x.blockStorage().volumes().get(uuid);

//Isoliated from UI thread
pool.submit( ()->{
    OSClient.OSClientV3 client = getClient(); //Provided by Openstack Contract
    return client.apply(action);
    });

标签:Multi,Sharing,Thread,thread,Token,token,client,Openstack
From: https://www.cnblogs.com/slankka/p/17404564.html

相关文章

  • JAVA基础(多线程Thread和Runnable的使用区别
    [color=red][size=x-large]两种定义方式[/size][/color]定义方式一:classTestThreadextendsThread{publicvoidrun(){........................}}Threadt=newTestThread();t.run()//或者t.start();定义方式二:Threadt=newRunnabl......
  • threadlocal使用场景
    1 在拦截器中将用户信息放到threadlocal中,后续业务方法直接从threadlocal中获取,这样做可以简化用户信息在方法间来回传递2 将类的成员变量放到threadlocal中,这个类在单例模式下也不会有线程安全问题.注意:使用完threadlocal后要调用remove()防止内存泄漏......
  • ThreadPoolExecutor的使用
    线程池的基类是concurrent.futures模块中的Executor,Executor提供了两个子类,即ThreadPoolExecutor和ProcessPoolExecutor,其中ThreadPoolExecutor用于创建线程池,而ProcessPoolExecutor用于创建进程池。如果使用线程池/进程池来管理并发编程,那么只要将相应的task函数提......
  • Thread类的方法
    Thread类的方法staticvoidyield():线程让步暂停当前正在执行的线程,把执行机会让给优先级相同或更高的线程若队列中没有同优先级的线程,忽略此方法join():当某个程序执行流中调用其他线程的join()方法时,调用线程将被阻塞,直到join()方法加入的join线程执行完为止......
  • ThreadPoolExecutor获取线程池中已经运行完的任务结果
    方法一:使用as_completed函数fromconcurrent.futuresimportThreadPoolExecutor,as_completedfromtimeimportsleepdefmethod(times):sleep(times)print('sleep{}secondes'.format(times))returntimespool=ThreadPoolExecutor(max_wor......
  • spring线程池ThreadPoolTaskExecutor
    1.自定义yml属性配置thread:pool:corePoolSize:5maxPoolSize:10queueCapacity:10keepAliveSeconds:1202.自定义线程池配置类@Configuration//配置类@EnableAsync//开线线程异步支持publicclassMyThreadPoolExecutor{@Autowiredprivat......
  • thread 中 join 和 detach 函数
    在C++中,库提供了多线程编程的功能,其中包含了std::thread类,该类用于创建和控制线程。std::thread类提供了两个重要的成员函数:join()和detach(),用于管理线程的生命周期。join()函数:join()函数用于等待线程执行完成,并将线程的执行结果合并到当前线程。换句话说,join()函数会阻塞当前......
  • 进程,multiprocessing模块
    整个系统进行资源分配调度的最小单位指正在执行的程序程序执行过程中一次指令,也可以交程序的一次执行过程进程是一种动态概念进程由三大部分组成代码段数据段PCB进程管理控制进程的三大基本状态就绪状态已获得运行需要的所有资源除了CPU执行状态已获得运行需......
  • Lagrange Multiplier Method
    LagrangeMultiplierMethod目录LagrangeMultiplierMethodPrerequisiteknowledge-partialderivativesUsageExSummaryTheEndofInequality:"\(\textsf{LagrangeMultiplierMethod}\)"Prerequisiteknowledge-partialderivativesInanutshell:pri......
  • AtCoder Regular Contest 129 C Multiple of 7
    洛谷传送门AtCoder传送门首先\(\text{7777...777}\)(\(x\)个\(7\))对能被\(7\)整除子串数量的贡献是\(\frac{x(x+1)}{2}\)。把\(n\)分解成若干\(x_i\)使得\(\sum\limits_{i=1}^m\frac{x_i(x_i+1)}{2}=n\),表示每段\(x_i\)个\(7\)。怎么把它们组合在一起呢?一个......