-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathcontainer_invalidation_spark.py
More file actions
50 lines (39 loc) · 2.4 KB
/
Copy pathcontainer_invalidation_spark.py
File metadata and controls
50 lines (39 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from pyspark.sql.functions import col, collect_list, concat_ws
import click as click
from CMSSpark.spark_utils import get_spark_session
from hadoop_queries import get_df_rse_locks, get_df_rse_replicas, get_df_contents
from pyspark.sql.window import Window
@click.command()
@click.option('--filename', required=True, default=None, type=str,
help='Name of the text file having the datasets names')
@click.option('--rse', required=False, default=None, type=str,
help='RSE to look at')
def invalidate_containers(filename,rse):
spark = get_spark_session(app_name='global_containers_invalidation')
#Read the containers to delete
filename = f'/user/dmtops/{filename}'
df_delete = spark.read.text(filename)
df_delete = df_delete.withColumnRenamed('value','CONTAINER')
#Get the basic df
df_locks = get_df_rse_locks(spark)
df_replicas = get_df_rse_replicas(spark,rse)
df_contents = get_df_contents(spark).alias('co')
#Get the content of the containers to delete (content includes filename, dataset and container)
df_delete = df_delete.join(df_contents,df_delete.CONTAINER==df_contents.CONTAINER,how='inner').select(['co.*']).alias('de')
#Replicas to declare as bad
df_delete = df_delete.join(df_replicas,df_delete.FILENAME==df_replicas.NAME,how='inner').select(['de.*','RSE','REPLICA_STATE']).alias('de')
#Rules protecting the replicas
df_delete = df_delete.join(df_locks,(df_delete.FILENAME==df_locks.NAME) & (df_delete.RSE == df_locks.RSE),how='left').select(['de.*','RULE_ID']).alias('de')
df_delete.cache()
#Files to invalidate on DBS
df_delete.select('FILENAME').distinct().toPandas().to_csv('/input/dbs_files_inv.txt',index=False, header = False)
windowSpec = Window.partitionBy('FILENAME')
df_delete.withColumn("RSES", collect_list(col("RSE")).over(windowSpec)) \
.select(['FILENAME','RSES']).withColumn("RSES", concat_ws(";", "RSES")).distinct().toPandas().to_csv('/input/rucio_replicas_inv.csv',index=False)
#Replicas to erase from Rucio
df_delete.select('DATASET').distinct().toPandas().to_csv('/input/datasets_inv.txt',index=False,header=False)
#RSE is exported in case it's tape and require purge_replicas
df_delete.filter(col('RULE_ID').isNotNull()).select(['RULE_ID','RSE']).distinct()\
.toPandas().to_csv('/input/rucio_rules_delete.csv',index=False)
if __name__ == "__main__":
invalidate_containers()