hdfs-native 0.13.5

Native HDFS client implementation in Rust
Documentation
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
use std::collections::HashMap;
use std::env;
use std::fs;
use std::net::ToSocketAddrs;
use std::path::{Path, PathBuf};

use dns_lookup::lookup_addr;
use log::debug;
use rand::rng;
use rand::seq::SliceRandom;

use crate::Result;

const HADOOP_CONF_DIR: &str = "HADOOP_CONF_DIR";
const HADOOP_HOME: &str = "HADOOP_HOME";

pub(crate) const DEFAULT_FS: &str = "fs.defaultFS";

// Name Service settings
const HA_NAMENODES_PREFIX: &str = "dfs.ha.namenodes";
const HA_NAMENODE_RPC_ADDRESS_PREFIX: &str = "dfs.namenode.rpc-address";
const DFS_CLIENT_FAILOVER_RESOLVE_NEEDED: &str = "dfs.client.failover.resolve-needed";
const DFS_CLIENT_FAILOVER_RESOLVER_USE_FQDN: &str = "dfs.client.failover.resolver.useFQDN";
const DFS_CLIENT_FAILOVER_RANDOM_ORDER: &str = "dfs.client.failover.random.order";
const DFS_CLIENT_FAILOVER_PROXY_PROVIDER: &str = "dfs.client.failover.proxy.provider";
const DFS_DATA_TRANSFER_PROTECTION: &str = "dfs.data.transfer.protection";

const HADOOP_SECURITY_AUTHENTICATION: &str = "hadoop.security.authentication";

// Viewfs settings
const VIEWFS_MOUNTTABLE_PREFIX: &str = "fs.viewfs.mounttable";

const DFS_CLIENT_WRITE_REPLACE_DATANODE_ON_FAILURE_ENABLE_KEY: &str =
    "dfs.client.block.write.replace-datanode-on-failure.enable";
const DFS_CLIENT_WRITE_REPLACE_DATANODE_ON_FAILURE_POLICY_KEY: &str =
    "dfs.client.block.write.replace-datanode-on-failure.policy";
const DFS_CLIENT_WRITE_REPLACE_DATANODE_ON_FAILURE_BEST_EFFORT_KEY: &str =
    "dfs.client.block.write.replace-datanode-on-failure.best-effort";

#[derive(Debug, Clone)]
pub(crate) struct Configuration {
    map: HashMap<String, String>,
}

impl Configuration {
    pub(crate) fn new(
        conf_dir: Option<String>,
        conf_map: Option<HashMap<String, String>>,
    ) -> Result<Self> {
        let mut map = HashMap::new();

        if let Some(conf_dir) = Self::parse_conf_dir(conf_dir) {
            map = Self::parse_conf(conf_dir)?;
        }

        if let Some(conf_map) = conf_map {
            map.extend(conf_map);
        }

        Ok(Configuration { map })
    }

    /// Get a value from the config, returning None if the key wasn't defined.
    pub fn get(&self, key: &str) -> Option<&str> {
        self.map.get(key).map(|s| s.as_ref())
    }

    fn get_boolean(&self, key: &str, default: bool) -> bool {
        self.get(key)
            .map(|v| v.to_lowercase() == "true")
            .unwrap_or(default)
    }

    pub(crate) fn security_enabled(&self) -> bool {
        self.get(HADOOP_SECURITY_AUTHENTICATION)
            .is_some_and(|c| c != "simple")
    }

    pub(crate) fn data_transfer_protection_enabled(&self) -> bool {
        self.get(DFS_DATA_TRANSFER_PROTECTION).is_some()
    }

    pub(crate) fn get_urls_for_nameservice(&self, nameservice: &str) -> Result<Vec<String>> {
        let urls: Vec<String> = self
            .map
            .get(&format!("{HA_NAMENODES_PREFIX}.{nameservice}"))
            .into_iter()
            .flat_map(|namenodes| {
                namenodes.split(',').flat_map(|namenode_id| {
                    self.map
                        .get(&format!(
                            "{HA_NAMENODE_RPC_ADDRESS_PREFIX}.{nameservice}.{namenode_id}"
                        ))
                        .map(|s| s.to_string())
                })
            })
            .collect();

        let mut urls = if self.get_boolean(
            &format!("{DFS_CLIENT_FAILOVER_RESOLVE_NEEDED}.{nameservice}"),
            false,
        ) {
            let use_fqdn = self.get_boolean(
                &format!("{DFS_CLIENT_FAILOVER_RESOLVER_USE_FQDN}.{nameservice}"),
                true,
            );

            let mut resolved_urls: Vec<String> = Vec::new();
            for url in urls {
                for socket_addr in url.to_socket_addrs()? {
                    if socket_addr.is_ipv4() {
                        if use_fqdn {
                            let fqdn = lookup_addr(&socket_addr.ip())?;
                            resolved_urls.push(format!("{}:{}", fqdn, socket_addr.port()));
                        } else {
                            resolved_urls.push(socket_addr.to_string());
                        }
                    }
                }
            }
            debug!(
                "Namenodes for {} resolved to {:?}",
                nameservice, resolved_urls
            );

            resolved_urls
        } else {
            debug!("Namenodes for {} without resolving {:?}", nameservice, urls);
            urls
        };

        if self.get_boolean(
            &format!("{DFS_CLIENT_FAILOVER_RANDOM_ORDER}.{nameservice}"),
            false,
        ) {
            urls.shuffle(&mut rng());
        }
        Ok(urls)
    }

    pub(crate) fn get_proxy_for_nameservice(&self, nameservice: &str) -> Option<&str> {
        self.get(&format!(
            "{DFS_CLIENT_FAILOVER_PROXY_PROVIDER}.{nameservice}"
        ))
    }

    pub(crate) fn get_mount_table(&self, cluster: &str) -> Vec<(Option<String>, String)> {
        self.map
            .iter()
            .flat_map(|(key, value)| {
                if let Some(path) =
                    key.strip_prefix(&format!("{VIEWFS_MOUNTTABLE_PREFIX}.{cluster}.link."))
                {
                    Some((Some(path.to_string()), value.to_string()))
                } else if key == &format!("{VIEWFS_MOUNTTABLE_PREFIX}.{cluster}.linkFallback") {
                    Some((None, value.to_string()))
                } else {
                    None
                }
            })
            .collect()
    }

    /// Get the replace datanode on failure policy from configuration
    pub fn get_replace_datanode_on_failure_policy(
        &self,
    ) -> crate::hdfs::replace_datanode::ReplaceDatanodeOnFailure {
        use crate::hdfs::replace_datanode::{Policy, ReplaceDatanodeOnFailure};

        let enabled = self.get_boolean(
            DFS_CLIENT_WRITE_REPLACE_DATANODE_ON_FAILURE_ENABLE_KEY,
            true,
        );
        if !enabled {
            return ReplaceDatanodeOnFailure::new(Policy::Disable, false);
        }

        let best_effort = self.get_boolean(
            DFS_CLIENT_WRITE_REPLACE_DATANODE_ON_FAILURE_BEST_EFFORT_KEY,
            false,
        );

        let policy_str = self
            .get(DFS_CLIENT_WRITE_REPLACE_DATANODE_ON_FAILURE_POLICY_KEY)
            .unwrap_or("DEFAULT")
            .to_uppercase();

        let policy = match policy_str.as_str() {
            "NEVER" => Policy::Never,
            "DEFAULT" => Policy::Default,
            "ALWAYS" => Policy::Always,
            _ => Policy::Default,
        };

        ReplaceDatanodeOnFailure::new(policy, best_effort)
    }

    fn read_from_file(path: &Path) -> Result<Vec<(String, String)>> {
        let content = fs::read_to_string(path)?;

        let resolver = EntityResolver::new(path)?;
        let entity_resolver = |_: Option<&str>, uri: &str| resolver.resolve(uri);
        let opts = roxmltree::ParsingOptions {
            allow_dtd: true,
            entity_resolver: Some(&entity_resolver),
            ..Default::default()
        };
        let tree = roxmltree::Document::parse_with_options(&content, opts)?;

        let pairs = tree
            .root()
            .children()
            .find(|d| d.tag_name().name() == "configuration")
            .into_iter()
            .flat_map(|config| {
                config
                    .children()
                    .filter(|c| c.tag_name().name() == "property")
            })
            .flat_map(|property| {
                let name = property.children().find(|n| n.tag_name().name() == "name");
                let value = property.children().find(|n| n.tag_name().name() == "value");

                match (name, value) {
                    (Some(name), Some(value)) => match (name.text(), value.text()) {
                        (Some(name), Some(text)) => Some((name.to_string(), text.to_string())),
                        _ => None,
                    },
                    _ => None,
                }
            });

        Ok(pairs.collect())
    }

    fn parse_conf_dir(conf_dir: Option<String>) -> Option<PathBuf> {
        if let Some(conf_dir) = conf_dir {
            return Some(PathBuf::from(conf_dir));
        }

        match env::var(HADOOP_CONF_DIR) {
            Ok(dir) => Some(PathBuf::from(dir)),
            Err(_) => match env::var(HADOOP_HOME) {
                Ok(dir) => Some([&dir, "etc/hadoop"].iter().collect()),
                Err(_) => None,
            },
        }
    }

    fn parse_conf(conf_dir: PathBuf) -> Result<HashMap<String, String>> {
        let mut map: HashMap<String, String> = HashMap::new();

        for file in ["core-site.xml", "hdfs-site.xml"] {
            let config_path = conf_dir.join(file);
            if config_path.as_path().exists() {
                Self::read_from_file(config_path.as_path())?
                    .into_iter()
                    .for_each(|(key, value)| {
                        map.insert(key, value);
                    })
            }
        }

        Ok(map)
    }
}

/// A simple entity resolver to resolve the XML system entities.
struct EntityResolver {
    basepath: PathBuf,
    bump: bumpalo::Bump,

    file_length_limit: u64,
    allocated_size_limit: u64,
}

impl EntityResolver {
    fn new(config_file_path: &Path) -> Result<Self> {
        let config_file_path = config_file_path.canonicalize()?;
        let basepath = match config_file_path.parent() {
            Some(p) => p.to_path_buf(),
            None => {
                // This should never happen, because the `config_file_path` is
                // expected to be a file. And so its parent should be a valid
                // directory.
                return Err(crate::HdfsError::InvalidPath(format!(
                    "invalid base path for configuration file: {}",
                    config_file_path.display()
                )));
            }
        };

        Ok(Self {
            basepath,
            bump: bumpalo::Bump::new(),

            file_length_limit: 16 * 1024 * 1024,    // 16 MiB
            allocated_size_limit: 16 * 1024 * 1024, // 16 MiB
        })
    }

    fn resolve<'a>(&'a self, uri: &str) -> core::result::Result<Option<&'a str>, String> {
        // Load full path.
        let full_path = self.resolve_full_path(uri)?;

        // Make sure the file exists.
        if !full_path.exists() {
            return Ok(None);
        }

        // Get metadata.
        let entity_file_metadata = match fs::metadata(&full_path) {
            Ok(m) => m,
            Err(e) => {
                return Err(format!(
                    "failed to get metadata of entity file {}: {}",
                    full_path.display(),
                    e
                ));
            }
        };
        let entity_file_size = entity_file_metadata.len();

        // Make sure the file size is reasonable.
        if entity_file_size > self.file_length_limit {
            return Err(format!(
                "entity file {} is too large ({} bytes)",
                full_path.display(),
                entity_file_size
            ));
        }

        // Make sure the allocated size is reasonable.
        let entity_file_allocated_size = self.bump.allocated_bytes() as u64;
        if entity_file_allocated_size + entity_file_size > self.allocated_size_limit {
            return Err(format!(
                "entity resolver has no more memory (allocated {} bytes, entity file size {} bytes)",
                entity_file_allocated_size, entity_file_size,
            ));
        }

        // Read the file content and move it into the bump arena.
        let entity_file_content = fs::read_to_string(&full_path).map_err(|e| {
            format!(
                "read entity file content (path {}): {}",
                full_path.display(),
                e
            )
        })?;
        let entity_file_content = self.bump.alloc_str(&entity_file_content);

        // Return the content.
        Ok(Some(entity_file_content))
    }

    fn resolve_full_path(&self, uri: &str) -> core::result::Result<PathBuf, String> {
        use std::path::{Component, Path};

        // Build the full path to the entity file.
        //
        // To make sure it is safe, we only allow relative path with `.` or
        // normal components.
        let path = Path::new(uri);
        let mut full_path = self.basepath.clone();
        for c in path.components() {
            match c {
                Component::CurDir => {}
                Component::ParentDir => {
                    return Err(
                        "parent directory components `..` are not allowed in entity URIs"
                            .to_string(),
                    );
                }
                Component::Normal(p) => full_path.push(p),
                Component::RootDir => {
                    return Err("absolute paths are not allowed in entity URIs".to_string());
                }
                Component::Prefix(p) => {
                    return Err(format!(
                        "absolute paths (with prefix {p:?}) are not allowed in entity URIs"
                    ));
                }
            }
        }

        Ok(full_path)
    }
}

#[cfg(test)]
mod test {
    use std::collections::HashMap;
    use std::net::IpAddr;

    use dns_lookup::lookup_addr;

    use crate::common::config::DFS_CLIENT_FAILOVER_RESOLVER_USE_FQDN;

    use super::{
        Configuration, DFS_CLIENT_FAILOVER_RESOLVE_NEEDED,
        DFS_CLIENT_WRITE_REPLACE_DATANODE_ON_FAILURE_BEST_EFFORT_KEY,
        DFS_CLIENT_WRITE_REPLACE_DATANODE_ON_FAILURE_ENABLE_KEY,
        DFS_CLIENT_WRITE_REPLACE_DATANODE_ON_FAILURE_POLICY_KEY, EntityResolver,
        HA_NAMENODE_RPC_ADDRESS_PREFIX, HA_NAMENODES_PREFIX, VIEWFS_MOUNTTABLE_PREFIX,
    };

    #[test]
    fn test_mount_table_config() {
        let mounts = [
            ("clusterX", "/view1", "/hdfs1"),
            ("clusterX", "/view2", "/hdfs2"),
            ("clusterY", "/view3", "/hdfs3"),
        ];

        let fallbacks = [("clusterX", "/hdfs4"), ("clusterY", "/hdfs5")];

        let config = Configuration {
            map: mounts
                .iter()
                .map(|(cluster, viewfs_path, hdfs_path)| {
                    (
                        format!("{VIEWFS_MOUNTTABLE_PREFIX}.{cluster}.link.{viewfs_path}"),
                        format!("hdfs://127.0.0.1:9000{hdfs_path}"),
                    )
                })
                .chain(fallbacks.iter().map(|(cluster, hdfs_path)| {
                    (
                        format!("{VIEWFS_MOUNTTABLE_PREFIX}.{cluster}.linkFallback"),
                        format!("hdfs://127.0.0.1:9000{hdfs_path}"),
                    )
                }))
                .collect(),
        };

        let mut mount_table = config.get_mount_table("clusterX");
        mount_table.sort();
        assert_eq!(
            vec![
                (None, "hdfs://127.0.0.1:9000/hdfs4".to_string()),
                (
                    Some("/view1".to_string()),
                    "hdfs://127.0.0.1:9000/hdfs1".to_string()
                ),
                (
                    Some("/view2".to_string()),
                    "hdfs://127.0.0.1:9000/hdfs2".to_string()
                )
            ],
            mount_table
        );

        let mut mount_table = config.get_mount_table("clusterY");
        mount_table.sort();
        assert_eq!(
            mount_table,
            vec![
                (None, "hdfs://127.0.0.1:9000/hdfs5".to_string()),
                (
                    Some("/view3".to_string()),
                    "hdfs://127.0.0.1:9000/hdfs3".to_string()
                )
            ]
        );
    }

    #[test]
    fn test_namenode_resolving() {
        let mut config = Configuration {
            map: vec![
                (
                    format!("{}.{}", HA_NAMENODES_PREFIX, "test"),
                    "namenode".to_string(),
                ),
                (
                    format!(
                        "{}.{}.{}",
                        HA_NAMENODE_RPC_ADDRESS_PREFIX, "test", "namenode"
                    ),
                    "localhost:9000".to_string(),
                ),
                (
                    format!("{}.{}", DFS_CLIENT_FAILOVER_RESOLVE_NEEDED, "test"),
                    "true".to_string(),
                ),
            ]
            .into_iter()
            .collect(),
        };

        let urls = config.get_urls_for_nameservice("test").unwrap();
        let fqdn = lookup_addr(&IpAddr::from([127, 0, 0, 1])).unwrap();
        assert_eq!(urls.len(), 1, "{urls:?}");
        assert_eq!(urls[0], format!("{fqdn}:9000"));

        config.map.insert(
            format!("{}.{}", DFS_CLIENT_FAILOVER_RESOLVER_USE_FQDN, "test"),
            "false".to_string(),
        );

        let urls = config.get_urls_for_nameservice("test").unwrap();
        assert_eq!(urls.len(), 1, "{urls:?}");
        assert_eq!(urls[0], "127.0.0.1:9000");
    }

    #[test]
    fn test_replace_datanode_policy_config() {
        // Test default policy
        let config = Configuration {
            map: HashMap::new(),
        };
        let policy = config.get_replace_datanode_on_failure_policy();
        assert!(!policy.is_best_effort());

        // Test disabled policy
        let config = Configuration {
            map: [(
                DFS_CLIENT_WRITE_REPLACE_DATANODE_ON_FAILURE_ENABLE_KEY.to_string(),
                "false".to_string(),
            )]
            .into_iter()
            .collect(),
        };
        let policy = config.get_replace_datanode_on_failure_policy();
        assert!(!policy.is_best_effort());

        // Test NEVER policy
        let config = Configuration {
            map: [
                (
                    DFS_CLIENT_WRITE_REPLACE_DATANODE_ON_FAILURE_ENABLE_KEY.to_string(),
                    "true".to_string(),
                ),
                (
                    DFS_CLIENT_WRITE_REPLACE_DATANODE_ON_FAILURE_POLICY_KEY.to_string(),
                    "NEVER".to_string(),
                ),
            ]
            .into_iter()
            .collect(),
        };
        let policy = config.get_replace_datanode_on_failure_policy();
        assert!(!policy.is_best_effort());

        // Test ALWAYS policy
        let config = Configuration {
            map: [
                (
                    DFS_CLIENT_WRITE_REPLACE_DATANODE_ON_FAILURE_ENABLE_KEY.to_string(),
                    "true".to_string(),
                ),
                (
                    DFS_CLIENT_WRITE_REPLACE_DATANODE_ON_FAILURE_POLICY_KEY.to_string(),
                    "ALWAYS".to_string(),
                ),
            ]
            .into_iter()
            .collect(),
        };
        let policy = config.get_replace_datanode_on_failure_policy();
        assert!(!policy.is_best_effort());

        // Test best-effort disabled
        let config = Configuration {
            map: [
                (
                    DFS_CLIENT_WRITE_REPLACE_DATANODE_ON_FAILURE_ENABLE_KEY.to_string(),
                    "true".to_string(),
                ),
                (
                    DFS_CLIENT_WRITE_REPLACE_DATANODE_ON_FAILURE_BEST_EFFORT_KEY.to_string(),
                    "false".to_string(),
                ),
            ]
            .into_iter()
            .collect(),
        };
        let policy = config.get_replace_datanode_on_failure_policy();
        assert!(!policy.is_best_effort());

        // Test best-effort enabled (explicit)
        let config = Configuration {
            map: [
                (
                    DFS_CLIENT_WRITE_REPLACE_DATANODE_ON_FAILURE_ENABLE_KEY.to_string(),
                    "true".to_string(),
                ),
                (
                    DFS_CLIENT_WRITE_REPLACE_DATANODE_ON_FAILURE_BEST_EFFORT_KEY.to_string(),
                    "true".to_string(),
                ),
            ]
            .into_iter()
            .collect(),
        };
        let policy = config.get_replace_datanode_on_failure_policy();
        assert!(policy.is_best_effort());

        // Test best-effort with invalid value (should use default false)
        let config = Configuration {
            map: [
                (
                    DFS_CLIENT_WRITE_REPLACE_DATANODE_ON_FAILURE_ENABLE_KEY.to_string(),
                    "true".to_string(),
                ),
                (
                    DFS_CLIENT_WRITE_REPLACE_DATANODE_ON_FAILURE_BEST_EFFORT_KEY.to_string(),
                    "invalid".to_string(),
                ),
            ]
            .into_iter()
            .collect(),
        };
        let policy = config.get_replace_datanode_on_failure_policy();
        assert!(!policy.is_best_effort()); // Invalid values treated as false

        // Test policy with both policy and best-effort config
        let config = Configuration {
            map: [
                (
                    DFS_CLIENT_WRITE_REPLACE_DATANODE_ON_FAILURE_ENABLE_KEY.to_string(),
                    "true".to_string(),
                ),
                (
                    DFS_CLIENT_WRITE_REPLACE_DATANODE_ON_FAILURE_POLICY_KEY.to_string(),
                    "ALWAYS".to_string(),
                ),
                (
                    DFS_CLIENT_WRITE_REPLACE_DATANODE_ON_FAILURE_BEST_EFFORT_KEY.to_string(),
                    "false".to_string(),
                ),
            ]
            .into_iter()
            .collect(),
        };
        let policy = config.get_replace_datanode_on_failure_policy();
        assert!(!policy.is_best_effort());
    }

    #[test]
    fn test_resolve_full_path() {
        use tempfile::tempdir;

        // Create a temporary directory for testing
        let temp_dir = tempdir().expect("Failed to create temp dir");
        let basepath = temp_dir.path().to_path_buf();

        let resolver = EntityResolver {
            basepath: basepath.clone(),
            bump: bumpalo::Bump::new(),
            file_length_limit: 16 * 1024 * 1024,
            allocated_size_limit: 16 * 1024 * 1024,
        };

        // Test normal relative path
        let result = resolver.resolve_full_path("config.xml");
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), basepath.join("config.xml"));

        // Test relative path with subdirectory
        let result = resolver.resolve_full_path("subdir/config.xml");
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), basepath.join("subdir").join("config.xml"));

        // Test path with current directory component
        let result = resolver.resolve_full_path("./config.xml");
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), basepath.join("config.xml"));

        // Test path with multiple current directory components
        let result = resolver.resolve_full_path("./subdir/./config.xml");
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), basepath.join("subdir").join("config.xml"));

        // Test path with parent directory component (should fail)
        let result = resolver.resolve_full_path("../config.xml");
        assert!(result.is_err());
        assert_eq!(
            result.unwrap_err(),
            "parent directory components `..` are not allowed in entity URIs"
        );

        // Test path with parent directory in the middle (should fail)
        let result = resolver.resolve_full_path("subdir/../config.xml");
        assert!(result.is_err());
        assert_eq!(
            result.unwrap_err(),
            "parent directory components `..` are not allowed in entity URIs"
        );

        // Test absolute path (should fail)
        let result = resolver.resolve_full_path("/absolute/path/config.xml");
        assert!(result.is_err());
        assert_eq!(
            result.unwrap_err(),
            "absolute paths are not allowed in entity URIs"
        );

        // Test absolute path (should fail)
        let result = resolver.resolve_full_path("/etc/passwd");
        assert!(result.is_err());
        assert_eq!(
            result.unwrap_err(),
            "absolute paths are not allowed in entity URIs"
        );

        // Test empty path
        let result = resolver.resolve_full_path("");
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), basepath);

        // Test path with only current directory
        let result = resolver.resolve_full_path(".");
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), basepath);

        // Test nested subdirectories
        let result = resolver.resolve_full_path("dir1/dir2/dir3/config.xml");
        assert!(result.is_ok());
        assert_eq!(
            result.unwrap(),
            basepath
                .join("dir1")
                .join("dir2")
                .join("dir3")
                .join("config.xml")
        );
    }

    #[test]
    fn test_config_read_from_file() {
        use indoc::indoc;
        use std::fs;
        use tempfile::tempdir;

        let temp_dir = tempdir().expect("Failed to create temp dir");

        // The main config file.
        let config_path = temp_dir.path().join("test-config.xml");
        let config_content = indoc! { r#"
            <?xml version="1.0"?>
            <!DOCTYPE configuration [
                <!ENTITY example-entity SYSTEM "example-entity.xml">
            ]>
            <configuration>
                <property>
                    <name>fs.defaultFS</name>
                    <value>hdfs://localhost:9000</value>
                </property>
                <property>
                    <name>dfs.replication</name>
                    <value>3</value>
                </property>
                &example-entity;
            </configuration>
        "# };
        fs::write(&config_path, config_content).expect("Failed to write config file");

        // The entity file.
        let example_entity_path = temp_dir.path().join("example-entity.xml");
        let example_entity_content = indoc! { r#"
            <?xml version="1.0"?>
            <property>
                <name>custom.property</name>
                <value>entity-value</value>
            </property>
        "# };
        fs::write(example_entity_path, example_entity_content)
            .expect("Failed to write entity file");

        // Get the config map from files.
        let map: HashMap<String, String> = {
            let pairs =
                Configuration::read_from_file(&config_path).expect("Failed to read config file");
            let mut res = HashMap::new();
            for (key, value) in pairs {
                res.insert(key, value);
            }
            res
        };
        assert_eq!(
            map.get("fs.defaultFS").map(|s| s.as_str()),
            Some("hdfs://localhost:9000")
        );
        assert_eq!(map.get("dfs.replication").map(|s| s.as_str()), Some("3"));
        assert_eq!(
            map.get("custom.property").map(|s| s.as_str()),
            Some("entity-value")
        );
    }
}