Skip to content

Commit a3ba2fe

Browse files
committed
chore: fix commen
1 parent 25c73af commit a3ba2fe

File tree

2 files changed

+21
-78
lines changed

2 files changed

+21
-78
lines changed

src/query/sharing-endpoint/src/accessor.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use std::sync::Arc;
16-
17-
use common_base::base::Singleton;
15+
use common_base::base::GlobalInstance;
1816
use common_exception::Result;
1917
use common_storage::init_operator;
2018
use common_storage::StorageParams;
21-
use once_cell::sync::OnceCell;
2219
use opendal::Operator;
2320
use time::Duration;
2421

@@ -27,11 +24,9 @@ use crate::models;
2724
use crate::models::PresignFileResponse;
2825
use crate::models::SharedTableResponse;
2926

30-
static SHARING_ACCESSOR: OnceCell<Singleton<Arc<SharingAccessor>>> = OnceCell::new();
31-
3227
#[derive(Clone)]
3328
pub struct SharingAccessor {
34-
op: Arc<Operator>,
29+
op: Operator,
3530
config: Config,
3631
}
3732

@@ -63,25 +58,24 @@ pub fn truncate_root(root: String, loc: String) -> String {
6358
}
6459

6560
impl SharingAccessor {
66-
pub async fn init(cfg: &Config, v: Singleton<Arc<SharingAccessor>>) -> Result<()> {
67-
let op = init_operator(&cfg.storage.params)?;
68-
v.init(Arc::new(SharingAccessor {
69-
op: Arc::new(op),
70-
config: cfg.clone(),
71-
}))?;
61+
pub async fn init(cfg: &Config) -> Result<()> {
62+
GlobalInstance::set(Self::try_create(cfg).await?);
7263

73-
SHARING_ACCESSOR.set(v).ok();
7464
Ok(())
7565
}
7666

77-
// get singleton instance for Sharing Accessor
78-
pub fn instance() -> Arc<SharingAccessor> {
79-
match SHARING_ACCESSOR.get() {
80-
None => panic!("Sharing Accessor is not init"),
81-
Some(sharing_accessor) => sharing_accessor.get(),
82-
}
67+
pub fn instance() -> SharingAccessor {
68+
GlobalInstance::get()
8369
}
8470

71+
pub async fn try_create(cfg: &Config) -> Result<SharingAccessor> {
72+
let operator = init_operator(&cfg.storage.params)?;
73+
74+
Ok(SharingAccessor {
75+
op: operator,
76+
config: cfg.clone(),
77+
})
78+
}
8579
fn get_root(&self) -> String {
8680
match self.config.storage.params {
8781
StorageParams::S3(ref s3) => s3.root.trim_matches('/').to_string(),

src/query/sharing-endpoint/src/services.rs

Lines changed: 7 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -12,73 +12,22 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use std::cell::UnsafeCell;
16-
use std::sync::Arc;
17-
18-
use common_base::base::GlobalIORuntime;
19-
use common_base::base::Runtime;
20-
use common_base::base::SingletonImpl;
15+
use common_base::base::GlobalInstance;
16+
use common_base::runtime::GlobalIORuntime;
2117
use common_exception::Result;
2218

2319
use crate::accessor::SharingAccessor;
2420
use crate::configs::Config;
2521

2622
// hold singleton services.
27-
pub struct SharingServices {
28-
// by default, operator init needs the global_runtime singleton instance
29-
// thus we keep it in the sharing services, alternatively, we may consider expose no layer operator
30-
global_runtime: UnsafeCell<Option<Arc<Runtime>>>,
31-
// storage_accessor is the accessor for the shared tenant data.
32-
storage_accessor: UnsafeCell<Option<Arc<SharingAccessor>>>,
33-
}
34-
35-
unsafe impl Send for SharingServices {}
36-
37-
unsafe impl Sync for SharingServices {}
23+
pub struct SharingServices {}
3824

3925
impl SharingServices {
4026
pub async fn init(config: Config) -> Result<()> {
41-
let sharing_services = Arc::new(SharingServices {
42-
global_runtime: UnsafeCell::new(None),
43-
storage_accessor: UnsafeCell::new(None),
44-
});
45-
GlobalIORuntime::init(config.storage.num_cpus as usize, sharing_services.clone())?;
46-
SharingAccessor::init(&config, sharing_services.clone()).await
47-
}
48-
}
49-
50-
impl SingletonImpl<Arc<SharingAccessor>> for SharingServices {
51-
fn get(&self) -> Arc<SharingAccessor> {
52-
unsafe {
53-
match &*self.storage_accessor.get() {
54-
None => panic!("Sharing Accessor is not init"),
55-
Some(storage_accessor) => storage_accessor.clone(),
56-
}
57-
}
58-
}
59-
60-
fn init(&self, value: Arc<SharingAccessor>) -> Result<()> {
61-
unsafe {
62-
*(self.storage_accessor.get() as *mut Option<Arc<SharingAccessor>>) = Some(value);
63-
Ok(())
64-
}
65-
}
66-
}
67-
68-
impl SingletonImpl<Arc<Runtime>> for SharingServices {
69-
fn get(&self) -> Arc<Runtime> {
70-
unsafe {
71-
match &*self.global_runtime.get() {
72-
None => panic!("Global Runtime is not init"),
73-
Some(runtime) => runtime.clone(),
74-
}
75-
}
76-
}
27+
// init global instance singleton
28+
GlobalInstance::init_production();
7729

78-
fn init(&self, value: Arc<Runtime>) -> Result<()> {
79-
unsafe {
80-
*(self.global_runtime.get() as *mut Option<Arc<Runtime>>) = Some(value);
81-
Ok(())
82-
}
30+
GlobalIORuntime::init(config.storage.num_cpus as usize)?;
31+
SharingAccessor::init(&config).await
8332
}
8433
}

0 commit comments

Comments
 (0)