From hdfs-issues-return-269170-archive-asf-public=cust-asf.ponee.io@hadoop.apache.org Mon Jun 24 21:37:02 2019 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [207.244.88.153]) by mx-eu-01.ponee.io (Postfix) with SMTP id EC350180671 for ; Mon, 24 Jun 2019 23:37:01 +0200 (CEST) Received: (qmail 3597 invoked by uid 500); 24 Jun 2019 21:37:01 -0000 Mailing-List: contact hdfs-issues-help@hadoop.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Delivered-To: mailing list hdfs-issues@hadoop.apache.org Received: (qmail 3586 invoked by uid 99); 24 Jun 2019 21:37:01 -0000 Received: from mailrelay1-us-west.apache.org (HELO mailrelay1-us-west.apache.org) (209.188.14.139) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 24 Jun 2019 21:37:01 +0000 Received: from jira-lw-us.apache.org (unknown [207.244.88.139]) by mailrelay1-us-west.apache.org (ASF Mail Server at mailrelay1-us-west.apache.org) with ESMTP id 77531E2AFF for ; Mon, 24 Jun 2019 21:37:00 +0000 (UTC) Received: from jira-lw-us.apache.org (localhost [127.0.0.1]) by jira-lw-us.apache.org (ASF Mail Server at jira-lw-us.apache.org) with ESMTP id 3D4482544B for ; Mon, 24 Jun 2019 21:37:00 +0000 (UTC) Date: Mon, 24 Jun 2019 21:37:00 +0000 (UTC) From: "ASF GitHub Bot (JIRA)" To: hdfs-issues@hadoop.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Work logged] (HDDS-1723) Create new OzoneManagerLock class MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 [ https://issues.apache.org/jira/browse/HDDS-1723?focusedWorklogId=3D2= 66137&page=3Dcom.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpan= el#worklog-266137 ] ASF GitHub Bot logged work on HDDS-1723: ---------------------------------------- Author: ASF GitHub Bot Created on: 24/Jun/19 21:36 Start Date: 24/Jun/19 21:36 Worklog Time Spent: 10m=20 Work Description: bharatviswa504 commented on pull request #1006: HDD= S-1723. Create new OzoneManagerLock class. URL: https://github.com/apache/hadoop/pull/1006#discussion_r296925887 =20 =20 ########## File path: hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/lo= ck/OzoneManagerLock.java ########## @@ -0,0 +1,312 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +package org.apache.hadoop.ozone.om.lock; + + +import java.util.ArrayList; +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.ozone.lock.LockManager; + +/** + * Provides different locks to handle concurrency in OzoneMaster. + * We also maintain lock hierarchy, based on the weight. + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
WEIGHT LOCK
0 S3 Bucket Lock
1 Volume Lock
2 Bucket Lock
3 User Lock
4 S3 Secret Lock
5 Prefix Lock
+ * + * One cannot obtain a lower weight lock while holding a lock with higher + * weight. The other way around is possible.
+ *
+ *

+ * For example: + *
+ * {@literal ->} acquire volume lock (will work)
+ * {@literal +->} acquire bucket lock (will work)
+ * {@literal +-->} acquire s3 bucket lock (will throw Exception)
+ *

+ *
+ */ + +public class OzoneManagerLock { + + private static final Logger LOG =3D + LoggerFactory.getLogger(OzoneManagerLock.class); + + private final LockManager manager; + private final ThreadLocal lockSet =3D ThreadLocal.withInitial( + () -> Short.valueOf((short)0)); + + + /** + * Creates new OzoneManagerLock instance. + * @param conf Configuration object + */ + public OzoneManagerLock(Configuration conf) { + manager =3D new LockManager<>(conf); + } + + /** + * Acquire lock on resource. + * + * For S3_Bucket, VOLUME, BUCKET type resource, same thread acquiring lo= ck + * again is allowed. + * + * For USER, PREFIX, S3_SECRET type resource, same thread acquiring lock + * again is not allowed. + * + * Special Note for UserLock: Single thread can acquire single user lock= / + * multi user lock. But not both at the same time. + * @param resourceName - Resource name on which user want to acquire loc= k. + * @param resource - Type of the resource. + */ + public void acquireLock(String resourceName, Resource resource) { + if (!resource.canLock(lockSet.get())) { + String errorMessage =3D getErrorMessage(resource); + LOG.error(errorMessage); + throw new RuntimeException(errorMessage); + } else { + manager.lock(resourceName); + lockSet.set(resource.setLock(lockSet.get())); + } + } + + private String getErrorMessage(Resource resource) { + return "Thread '" + Thread.currentThread().getName() + "' cannot " + + "acquire " + resource.name + " lock while holding " + + getCurrentLocks().toString() + " lock(s)."; + + } + + private List getCurrentLocks() { + List currentLocks =3D new ArrayList<>(); + int i=3D0; + short lockSetVal =3D lockSet.get(); + for (Resource value : Resource.values()) { + if ((lockSetVal & value.setMask) =3D=3D value.setMask) { + currentLocks.add(value.name); + } + } + return currentLocks; + } + + /** + * Acquire lock on multiple users. + * @param oldUserResource + * @param newUserResource + */ =20 Review comment: Done. =20 ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. =20 For queries about this service, please contact Infrastructure at: users@infra.apache.org Issue Time Tracking ------------------- Worklog Id: (was: 266137) Time Spent: 2h 40m (was: 2.5h) > Create new OzoneManagerLock class > --------------------------------- > > Key: HDDS-1723 > URL: https://issues.apache.org/jira/browse/HDDS-1723 > Project: Hadoop Distributed Data Store > Issue Type: Improvement > Components: Ozone Manager > Reporter: Bharat Viswanadham > Assignee: Bharat Viswanadham > Priority: Major > Labels: pull-request-available > Time Spent: 2h 40m > Remaining Estimate: 0h > > This Jira is to use bit manipulation, instead of hashmap in OzoneManager = lock logic. And also this Jira follows the locking order based on the docum= ent attached to HDDS-1672 jira. > This Jira is created based on [~anu] comment during review of HDDS-1672. > Not a suggestion for this patch. But more of a question, should we just m= aintain a bitset here, and just flip that bit up and down to see if the loc= k is held. Or we can just maintain 32 bit integer, and we can easily find i= f a lock is held by Xoring with the correct mask. I feel that might be supe= r efficient.=C2=A0[@nandakumar131|https://github.com/nandakumar131]=C2=A0. = But as I said let us not do that in this patch. > =C2=A0 > This Jira will add new class, integration of this new class into code wil= l be done in a new jira.=C2=A0 > Clean up of old code also will be done in new jira. -- This message was sent by Atlassian JIRA (v7.6.3#76005) --------------------------------------------------------------------- To unsubscribe, e-mail: hdfs-issues-unsubscribe@hadoop.apache.org For additional commands, e-mail: hdfs-issues-help@hadoop.apache.org